This is kind of a lame question because I don’t have a problem. I just recently started learning about JSON, and I wanted to figure out how to convert a PDO database query to JSON.
To my surprise, my very first attempt worked…
$stm = $pdo->prepare("SELECT *
FROM $DBPolitix.pox_topics");
$stm->execute(array(
));
$results=$stm->fetchAll(PDO::FETCH_ASSOC);
$json=json_encode($results);
echo $json;
It appears to be displaying all the data from the table px_topics in a big jumbled mass that looks like this:
{“N”:“8”,“URL”:“political-compass”,“Title”:“Political Compass”,“Subtitle”:“The Political Spectrum”,“Parent”:“politix-101”,“MetaTitle”:“”,“MetaDesc”:“”,“KW”:“”,“Previous”:“topics/left-vs-right”,“Next”:“topics/morality”,“Site”:“PX”,“Section”:“Topics”,“Series”:“PX-101”,“TestID”:“PX-101-7”,“Category”:“”,“Age”:“”,“Live”:“1”,“G1”:“1”},{“N”:“9”,“URL”:“morality”,“Title”:“Morality”,“Subtitle”:“The Foundation of a Whole Person”,“Parent”:“politix-101”,“MetaTitle”:“”,“MetaDesc”:“”,“KW”:“”,“Previous”:“topics/political-compass”,“Next”:“topics/good-and-evil”,“Site”:“PX”,“Section”:“Topics”,“Series”:“PX-101”,“TestID”:“PX-101-8”,“Category”:“”,“Age”:“”,“Live”:“1”,“G1”:“1”},{“N”:“10”,“URL”:“good-and-evil”
But before I continue and learn how to put all that stuff in a table, I’d like to ask about the script I posted above. Is it really the best way to go, or is there some subtle change I should make, or do you have any recommendations for improving it?
All I want to do is recreate the database tables I’m currently displaying in a format that will allow me to display them via AJAX. Many of my queries feature table joins and WHERE clauses.
It’s just so strange for me to get something right on the first try, I want to make sure I’m not missing something before I continue.
Thanks.