MySQL到JSON列出多行

时间:2015-06-17 00:44:48

标签: php mysql json

我目前正在开发一个PHP脚本,它将用户数据编码为JSON,以及用户可能执行的相关操作/事件。例如,用户数据中的某些字段(在“USER”MySQL表中定义)包括用户的名字和姓氏,网站登录以及他们完成的某些操作的点数。在第二个表中,称为“MAYDO”,用户执行的操作被存储(由ID引用),并包括诸如用户所做的信息(去了星巴克,购买X,在日期Y和日期之间执行了操作)。 Z等等)

我的问题是,如何引用我的'MAYDO'表中的每个元素,并以JSON列表的形式将其与正确的用户相关联? 我希望有这样的功能:

{
    'USER' {
        'Name': 'John Doe',
        'Occupation': 'Farmer',
        'Age': 39
        'User_id': 1
     },
     'MAYDO' [{
          'User_Id': 1,
          'Maydo_Id': 1,
          'Event': 'Go to Farmer\'s Market',
          'When': '2015-10-13 16:30:05'
      },
      {
          'User_Id': 1,
          'Maydo_Id': 2,
          'Event': 'Sell chickens at the auction',
          'When': '2015-11-13 12:00:00'
      }]
}

基本上,我希望'MAYDO'表中的所有行与执行它们的各自用户相关联,并且基本上成为该用户的JSON数据的列表(每人一个JSON文件)。我目前测试的代码只接受'MAYDO'表中的最后一个条目(如果发生多个事件),所以我想了解如何解决这个问题。任何帮助或提示表示赞赏。谢谢!

当前代码(完美地编码用户数据;仅编码最后一个'MAYDO'条目)

<!doctype html>
<html lang="en"> 
<head>
    <meta charset="UTF-8">
    <title>USER DATA TO JSON</title>
</head>
<body>

<?php

# Define the connection to the database 
DEFINE('DB_SERVER', 'localhost');
DEFINE('DB_USER', 'root');
DEFINE('DB_PASSWORD', ''); 
DEFINE('DB_NAME', 'Maydo');

# Create a connection to the database
$connection = @mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME);
$index = 1;
$query1 = "SELECT * FROM USER WHERE USER_ID = " . $index;
$userinfo = array();
$username = "";

$result1 = mysqli_query($connection, $query1) or die("ERROR: " .      
mysqli_error($connection));

while($row = mysqli_fetch_assoc($result1)) {
    $userinfo['User'] = $row;
    $username = $row['USER_NAME']; 
}

$query2 = "SELECT * FROM MAYDO WHERE USER_ID = " . $index;

$result2 = mysqli_query($connection, $query2) or die("ERROR: " .    
mysqli_error($connection));
while($row = mysqli_fetch_assoc($result2)) {
    $userinfo['Maydo'] = array($row);
}

echo json_encode($userinfo, JSON_NUMERIC_CHECK);

?> 

</body>
</html>

1 个答案:

答案 0 :(得分:0)

尝试类似这样的内容,请参阅此有用的链接    http://php.net/manual/en/function.array-push.php
   http://php.net/manual/en/function.json-encode.php

  <?php

    # Define the connection to the database 
    DEFINE('DB_SERVER', 'localhost');
    DEFINE('DB_USER', 'root');
    DEFINE('DB_PASSWORD', ''); 
    DEFINE('DB_NAME', 'Maydo');

    # Create a connection to the database
    $connection = @mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME);
    $index = 1;
    $query1 = "SELECT * FROM USER WHERE USER_ID = " . $index;
    $query2 = "SELECT * FROM MAYDO WHERE USER_ID = " . $index;

    $userinfo = array();
    $username = "";

    $result1 = mysqli_query($connection, $query1) or die("ERROR: " .      
    mysqli_error($connection));

    $result2 = mysqli_query($connection, $query2) or die("ERROR: " .    
    mysqli_error($connection));

    $mayDo = array();
    while($row = mysqli_fetch_assoc($result1)) {

        while($row2 = mysqli_fetch_assoc($result2)) {
          array_push($mayDo,$row2);
        }
        array_push($row,$mayDo);
        array_push($userinfo,$row);
    }

    echo json_encode($userinfo, JSON_NUMERIC_CHECK);

    ?>