Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP, JSON, and MySQL

Tags:

json

php

mysql

What is the easiest way to retrieve data from the database, and convert the data to a JSON String?

Is there any kind of helper class? or am I supposed to loop through the columns in my dataset to create the JSON string?

like image 877
aryaxt Avatar asked Aug 01 '26 20:08

aryaxt


1 Answers

You can use the json_encode function to convert a native PHP array or stdClass object to it's corresponding JSON representation:

$result = $db->query('SOME QUERY');
$set = array();
if($result->num_rows) {
   while($row = $result->fetch_array()) {
      $set[] = $row;
   }
}
echo json_encode($set);
like image 71
Jacob Relkin Avatar answered Aug 04 '26 09:08

Jacob Relkin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!