dory4404 2013-11-15 23:11 采纳率: 0%
浏览 37
已采纳

了解数据库表的JSON输出

I'm new to JSON and I am writing a PHP script to take all the data from a remote MySQL db to import to a ios sqlite database. My plan is to take all the tables from each database and send it to the other and check if the rowid are in the right table, if it is, throw it out, if not add it to that database, basically adding whatever rows aren't in the database to it.

I am trying to read my JSON output from the remote MySql server and I dont quite understand it and can't find any good resources. For my login table I pull it like this:

$query = "SELECT * from answers";
$result = $this->db->query($query) or die('Errant query:  '.$query);

$answers = array();
$answers=mysqli_fetch_array($result);
header('Content-type: application/json');
echo json_encode(array(
'login'=>$login,
'answers'=>$answers,
'projects'=>$projects,
'questions'=>$questions,
'surveys'=>$surveys,
 ));

Then when I read the output it shows for the login table:

  {"login":{"0":"1","userid":"1","1":"test","password":"test","2":"1","aclevel":"1"},

The login table has three columns: userid, password, and aclevel. Filled in this table is 1, test, 1 respectively.

Can someone explain to me what the output of the JSON means?

  • 写回答

1条回答 默认 最新

  • duanqiaoren9975 2013-11-15 23:17
    关注

    See the docs for mysql_fetch_array.

    It returns an array with both numeric and field index if the second parameter is missing. This generates an array like this:

    array (
        'field1' => value1,
        0 => value1,
        'field2' => value2,
        1 => value2,
        ...
    )
    

    Use:

    mysql_fetch_array($result, MYSQL_ASSOC);
    // OR...
    mysql_fetch_assoc($result);
    

    To retrieve data and see what happens.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?