[英]MySql products and categories tables using php and json
我刚刚开始学习所有这些编码语言,老师要求我们做以下事情。 我知道这对于全职工作或有更多时间编码的人来说确实很容易。 老师总是告诉我们GOOGLE的所有信息,我尝试了太多网站,但没有发现任何对我有帮助的东西。
我需要使用PHP编写两个JSON文档(产品和类别),这些文档将动态地从MySQL数据库读取值。 调用每个文档时,它将返回格式正确的JSON,该JSON将使用http://jsonlint.com/进行验证
老实说,我不知道该怎么办。 我不了解PHP,现在这个JSON事情使它变得更加混乱。
您只需要向数据库发出MySQL请求:
(这是简单的代码,尚未优化)
// 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
}
有了这个你有一个数组$rows
与您的数据:
之后,您可以将数据输出为JSON:
echo json_encode($rows);
希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.