note 116412 deleted from sqlite3.query by salathe
| From: | salathe@php.net | Date: | Fri, 02 Jan 2015 11:32:21 +0000 |
| Subject: | note 116412 deleted from sqlite3.query by salathe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-201331@lists.php.net to get a copy of this message | ||
Note Submitter: Mark G
----
Anyone wanting to count the number of rows returned by a query using SQL3 will need to do something
like this.
<code>$db = new SQLite3("problemswith.sql3");
// the query
$results = $db->query( "Select * FROM
wordlist WHERE
word='$searchword';" );
// the query to count results
$count = $db->query( "Select count(*) FROM wordlist WHERE
word='$searchword';" )->fetchArray()[0];
// the total number of records
$total = $db->query("SELECT count(*) FROM wordlist;")->fetchArray()[0];
echo "Found {$count} results from {$total} records.";</code>
It is important to note that your query string should be sanitized if you obtain the data from
outside your program like using $_GET and $_POST directly in any SQL query is not only risky but
also silly.