php - mysql 从 json 结果

标签 php mysql json

我有一个 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/

相关文章:

php - 由于命名空间,全局 "Call to undefined function"函数的 "curl_init"错误

php - 这个正则表达式在 codeigniter 的 uri 中不起作用

mysql - vb.net 中的通用搜索框并从多个表中进行检查

mysql - 进行 SQL 查询以给出如下所示的特定结果

php - 使用案例PDO全文搜索时如何按相关性排序

php - jquery 中的 Stack Overflow 样式标记系统

sql - mysql - 按一天中的小时搜索时间戳

ios - 在 IOS 中从 google map place api 中提取需要信息

javascript - Firebase - 获取值未知的子项的父项

javascript - 从 JavaScript 中的循环创建的对象,如何在 json 中分析它们