我有一个 mysql 数据库,我需要从中获取其内容的 rss feed。 我需要 json 中的等效项:
mysql_select_db("mydb", $con);
$result = mysql_query("select date, title, description, url from blah where type = 'OFFERS' order by ref desc");
echo '<?xml version="1.0"?>
<rss version="2.0">
<channel>
<title></title>
<description></description>
<link></link>';
while($row = mysql_fetch_array($result))
{
echo "
<item>
<title>" . $row['title'] . "</title>
<description>" . $row['description'] . "</description>
<link>" . $row['url'] . "</link>
<image>" . $row['date'] . "</image>
</item>";
}
到目前为止,我能开始工作的最接近的事情是:
<?php
$host="localhost";
$username="username";
$password="password";
$db_name="mydb";
$con=mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql = "select date, title, description, url from blah where type = 'OFFERS' order by ref desc";
$result = mysql_query($sql);
$json = array();
if(mysql_num_rows($result)){
while($row=mysql_fetch_row($result)){
$json['title'][]=$row;
}
}
mysql_close($db_name);
echo json_encode($json);
?>
如有任何帮助,我们将不胜感激
最佳答案
while ($row = mysql_fetch_assoc($result)) {
$json[] = $row;
}
echo json_encode($json);
关于php - mysql 从 json 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9941336/