itsdavyh itsdavyh - 4 months ago 9
PHP Question

SQLite3 returns both indexed and associative array

I have a SQLite query in PHP:

$database = new SQLite3("database.db");
$statement = $database->prepare("SELECT * FROM table");
$result = $statement->execute();
$table = array();
while ($row = $result->fetchArray()) {
array_push($table, $row);
}
var_dump($table);


and it outputs

array(2) {
[0]=> array(4) {
[0]=> int(1)
["event"]=> int(1)
[1]=> string(2) "A1"
["code"]=> string(2) "A1"
}
[1]=> array(4) {
[0]=> int(5)
["event"]=> int(5)
[1]=> string(2) "A2"
["code"]=> string(2) "A2"
}
}


Which is the correct data, but it's outputting all of the information twice: one with an index attached, and one with the column name. Is there any way to pick one or the other? This program needs to be very memory efficient as it will be expanded to having thousands of rows.

Answer

Stupid me should have looked at the documentation before I posted. SQLite3's fetchArray function can take a mode parameter. You can use SQLITE3_ASSOC, SQLITE3_NUM, or SQLITE3_BOTH for the desired effect.

Comments