Reputation: 17
I have just started learning all this coding language and the teacher is asking us to do the following. I know it may sound really easy for people who do this full time or have more time coding. The teacher is always telling us to GOOGLE everything and I have tried too many sites but I haven't found anything that helps me at all.
I need to write two JSON documents (products and categories) using PHP that will dynamically read values from my MySQL database. When each document is called upon, it will return perfectly formatted JSON that will validate using http://jsonlint.com/
Honestly I don't know what to do. I don't understand PHP and now this JSON thing is making it more confusing.
Upvotes: 0
Views: 462
Reputation: 3226
You just need to make a MySQL request to your database:
(this is simple code and not optimized)
// Make a MySQL Connection
mysql_connect("localhost", "admin", "1admin") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
// Retrieve the data from the "example" table
$result = mysql_query("SELECT products, categories FROM example")
or die(mysql_error());
// store all the records of the "example" table into $rows[]
while($ds = mysql_fetch_array( $result )) {
$rows[] = $ds
}
with this you have an array $rows
with your data:
after this you can output the data as JSON:
echo json_encode($rows);
I hope this will help you a little bit.
Upvotes: 1