I have an sqlite database as follows
id number
1 24
2 34
3 65
I would like to sum up the total of the number column and print the output. I’ve tried the following but get “sqlite_fetch_array() expects parameter 1 to be resource, object given”.
$database = new SQLiteDatabase('test.sqlite', 0666, $error);
$query = "SELECT SUM(Number) AS sums FROM Pushups";
$result = $database->query($query) or die($error);
$i = sqlite_fetch_array($result);
echo $i['sums'];
Could someone please show me the correct way to do this
Thanks
If you use the “object oriented” style for creating the database connection, you have to use it consistently for all methods. That means you can’t mix the object oriented interface (the
SQLite*-classes) and the procedural interface (the functions that start withsqlite_*).So if you use the
querymethod ofSQLiteDatabaseit returns anSQLiteResultobject instance (as defined in the docs forSQLiteDatabase::query). If you check out thesqlite_fetch_arraydocs you can see that in the object oriented style, you have to use theSQLiteResult::fetch-method.So your code should look like: