简体   繁体   中英

MySql products and categories tables using php and json

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM