【问题标题】:WordPress to .json file....no data appearingWordPress 到 .json 文件....没有数据出现
【发布时间】:2013-09-03 16:36:02
【问题描述】:

尝试将几个 WordPress 表格行从 wp_posts 转换为 .json 文件。不是我多次看到的 JSON API 插件。我已经尝试过了,它产生了太多的数据。我只想要 ID、标题和帖子或页面内容。就是这样。此外,JSON API 实际上并不导出 .json 文件。我想要在服务器上动态显示。

所以我使用这个脚本来生成 .json 文件。

<?php $con=mysql_connect("localhost","username","password") or die("Database connection failed");

if($con)
{
mysql_selectdb("database_wp",$con);
}
?>

<?php

$data=array();
$qa=array();
$query=mysql_query("SELECT id, title, content FROM wp_posts ORDER BY id");
while($geteach=mysql_fetch_array($query))
{
$id=$getdata[0];
$title=$getdata[1];
$content=$getdata[2];
$qa[]=array('id'=> $id, 'title'=> $title, 'content'=> $content);
}
$data['qa']=$qa;

$fp = fopen('myjson.json', 'w');
fwrite($fp, json_encode($data));
fclose($fp);

?>

但我在生成的 .json 文件中得到的只是 {"qa":[]} 而已。服务器上有数据。那我怎么拉不进去?解决方法是什么?

【问题讨论】:

    标签: php json wordpress api


    【解决方案1】:

    我认为问题出在您的 while 循环中。

    你使用

    while($geteach=mysql_fetch_array($query))
    {
      $id=$getdata[0];
      $title=$getdata[1];
      $content=$getdata[2];
      $qa[]=array('id'=> $id, 'title'=> $title, 'content'=> $content);
    }
    

    我认为应该是:

    while($getdata=mysql_fetch_array($query))
    {
      $id=$getdata[0];
      $title=$getdata[1];
      $content=$getdata[2];
      $qa[]=array('id'=> $id, 'title'=> $title, 'content'=> $content);
    }
    

    因为否则$getdata 是空的。

    【讨论】:

      【解决方案2】:

      为什么不使用 WP API 插件并以这种方式使用数据?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-17
        • 2016-08-26
        • 1970-01-01
        • 2017-10-01
        相关资源
        最近更新 更多