使用wordpress从php和mySql查询获取json输出

时间:2015-09-25 13:04:25

标签: php mysql json wordpress

我试图创建一个应该向我显示json输出的php页面。我在一个wordpress网站上,也许它与从数据库中检索数据有点不同普通的php页面。顺便说一句,这是我的代码:

<?php


    $path = $_SERVER['DOCUMENT_ROOT'];

    include_once $path . '/wp-config.php';
    include_once $path . '/wp-load.php';
    include_once $path . '/wp-includes/wp-db.php';
    include_once $path . '/wp-includes/pluggable.php';
    global $wpdb;

    $code = ''; 
    $code=$_REQUEST['code'];

    $timestamp = ''; 
    $timestamp=$_REQUEST['timestamp'];

    $username = ''; 
    $username=$_REQUEST['username'];

    $selectData = "SELECT * FROM protocol_data_list WHERE username = '$current_user->user_login'";
    //Create an array
    $json_response = array();
    $result = $wpdb->query($selectData);

    echo $result; 
    while ($row=mysqli_fetch_array($result, MYSQL_ASSOC)) {
    echo $row;
    $row_array['code'] = $row['code'];
    $row_array['timestamp'] = $row['timestamp'];
    $row_array['username'] = $row['username'];

        array_push($json_response,$row_array);
    }

    $json_response = str_replace('\\/', '/', json_encode(array('item' => $json_response), 128));

    echo $json_response;


?>

这种代码是我总是用来创建json的代码..但是在wordpress中它返回一个空数组..我不知道是否有错误。

0 个答案:

没有答案