I'm converting some existing code from mysqli to PDO to use prepared statements/bind variables to help prevent SQL injection on queries, but I've run into a problem with fetching the results and using json as input to JavaScript.
After executing the query, I use this code to build the results array:
$stmt->setFetchMode(PDO::FETCH_ASSOC);
while($row = $stmt->fetch()) {
$json[] = $row;
}
echo json_encode($json);
The problem is that it seems any of my database columns that are not strings are returned without double quotes around them, unlike when I used mysqli, which wrapped all values in double quotes. How do I fix this? Is there a way to get the results with all the values as strings?
Here's the comparison of results - note that 'ID' is my auto number column and in the PDO results it is NOT wrapped in double quotes:
PDO
[{"ID":55,"Entry_Form_Number":1005,"Barcode_Text":"2017-1005","Entrant_Name":"aa bb","Model_Name":"cc","Category_Name":"Dioramas","Paid":1,"DatePaid":"09-Feb-17 11:28AM","DateCreated":"03-Feb-17 01:11PM","DateLastUpdated":"09-Feb-17 11:28AM","LastUpdatedBy":"wf_boxoffice"},{"ID":56,"Entry_Form_Number":1006,"Barcode_Text":"2017-1006","Entrant_Name":"aa bb","Model_Name":"cc","Category_Name":"Dioramas","Paid":1,"DatePaid":"09-Feb-17 11:28AM","DateCreated":"03-Feb-17 01:16PM","DateLastUpdated":"09-Feb-17 11:28AM","LastUpdatedBy":"wf_boxoffice"},{"ID":59,"Entry_Form_Number":1009,"Barcode_Text":"2017-1009","Entrant_Name":"aa bb","Model_Name":"cc","Category_Name":"Original Sculpt/Scratchbuilt","Paid":1,"DatePaid":"09-Feb-17 11:28AM","DateCreated":"04-Feb-17 10:32AM","DateLastUpdated":"09-Feb-17 11:28AM","LastUpdatedBy":"wf_boxoffice"},{"ID":62,"Entry_Form_Number":1010,"Barcode_Text":"2017-1010","Entrant_Name":"aa bb","Model_Name":"cc","Category_Name":"Dioramas","Paid":1,"DatePaid":"09-Feb-17 11:28AM","DateCreated":"08-Feb-17 08:31PM","DateLastUpdated":"09-Feb-17 11:28AM","LastUpdatedBy":"wf_boxoffice"},{"ID":63,"Entry_Form_Number":1011,"Barcode_Text":"2017-1011","Entrant_Name":"aa bb","Model_Name":"cc","Category_Name":"Dinosaurs","Paid":0,"DatePaid":null,"DateCreated":"09-Feb-17 08:12PM","DateLastUpdated":"09-Feb-17 08:12PM","LastUpdatedBy":"wf_anon"},{"ID":64,"Entry_Form_Number":1012,"Barcode_Text":"2017-1012","Entrant_Name":"aa bb","Model_Name":"cc","Category_Name":"Horror Figures","Paid":0,"DatePaid":null,"DateCreated":"10-Feb-17 07:55AM","DateLastUpdated":"10-Feb-17 07:55AM","LastUpdatedBy":"wf_anon"}]
MYSQLI
[{"ID":"55","Entry_Form_Number":"1005","Barcode_Text":"2017-1005","Entrant_Name":"aa bb","Model_Name":"cc","Category_Name":"Dioramas","Paid":"1","DatePaid":"09-Feb-17 11:28AM","DateCreated":"03-Feb-17 01:11PM","DateLastUpdated":"09-Feb-17 11:28AM","LastUpdatedBy":"wf_boxoffice"},{"ID":"56","Entry_Form_Number":"1006","Barcode_Text":"2017-1006","Entrant_Name":"aa bb","Model_Name":"cc","Category_Name":"Dioramas","Paid":"1","DatePaid":"09-Feb-17 11:28AM","DateCreated":"03-Feb-17 01:16PM","DateLastUpdated":"09-Feb-17 11:28AM","LastUpdatedBy":"wf_boxoffice"},{"ID":"59","Entry_Form_Number":"1009","Barcode_Text":"2017-1009","Entrant_Name":"aa bb","Model_Name":"cc","Category_Name":"Original Sculpt/Scratchbuilt","Paid":"1","DatePaid":"09-Feb-17 11:28AM","DateCreated":"04-Feb-17 10:32AM","DateLastUpdated":"09-Feb-17 11:28AM","LastUpdatedBy":"wf_boxoffice"},{"ID":"62","Entry_Form_Number":"1010","Barcode_Text":"2017-1010","Entrant_Name":"aa bb","Model_Name":"cc","Category_Name":"Dioramas","Paid":"1","DatePaid":"09-Feb-17 11:28AM","DateCreated":"08-Feb-17 08:31PM","DateLastUpdated":"09-Feb-17 11:28AM","LastUpdatedBy":"wf_boxoffice"},{"ID":"63","Entry_Form_Number":"1011","Barcode_Text":"2017-1011","Entrant_Name":"aa bb","Model_Name":"cc","Category_Name":"Dinosaurs","Paid":"0","DatePaid":null,"DateCreated":"09-Feb-17 08:12PM","DateLastUpdated":"09-Feb-17 08:12PM","LastUpdatedBy":"wf_anon"},{"ID":"64","Entry_Form_Number":"1012","Barcode_Text":"2017-1012","Entrant_Name":"aa bb","Model_Name":"cc","Category_Name":"Horror Figures","Paid":"0","DatePaid":null,"DateCreated":"10-Feb-17 07:55AM","DateLastUpdated":"10-Feb-17 07:55AM","LastUpdatedBy":"wf_anon"}]