简体   繁体   English

使用php和json的MySql产品和类别表

[英]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. 老师总是告诉我们GOOGLE的所有信息,我尝试了太多网站,但没有发现任何对我有帮助的东西。

I need to write two JSON documents (products and categories) using PHP that will dynamically read values from my MySQL database. 我需要使用PHP编写两个JSON文档(产品和类别),这些文档将动态地从MySQL数据库读取值。 When each document is called upon, it will return perfectly formatted JSON that will validate using http://jsonlint.com/ 调用每个文档时,它将返回格式正确的JSON,该JSON将使用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. 我不了解PHP,现在这个JSON事情使它变得更加混乱。

You just need to make a MySQL request to your database: 您只需要向数据库发出MySQL请求:

(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: 有了这个你有一个数组$rows与您的数据:

after this you can output the data as JSON: 之后,您可以将数据输出为JSON:

echo json_encode($rows);

I hope this will help you a little bit. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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