Skip to content
Advertisement

convert sqlite data to json using php

I’m trying to convert sqlite query into json. I have the following table with two columns name and age. When I print the query the format doesn’t seem to be correct. why am I getting an extra key value pair?

JavaScript

output

JavaScript

desired output

JavaScript

Advertisement

Answer

Change query to get only those column which are required:

JavaScript

And then use SQLITE3_ASSOC

JavaScript

Reference:- SQLite3Result::fetchArray

Parameters

mode

Controls how the next row will be returned to the caller. This value must be one of either SQLITE3_ASSOC, SQLITE3_NUM, or SQLITE3_BOTH.

SQLITE3_ASSOC: returns an array indexed by column name as returned in the corresponding result set

SQLITE3_NUM: returns an array indexed by column number as returned in the corresponding result set, starting at column 0

SQLITE3_BOTH: returns an array indexed by both column name and number as returned in the corresponding result set, starting at column 0

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement