dongzong2017 2015-06-17 00:44
浏览 417

MySQL到JSON列出多行

I am currently working on a PHP script that encodes user data into JSON, as well as associated actions/events a user might perform. For example, some fields from the user data (defined in a 'USER' MySQL table) include the user's first and last name, website login, and points for certain actions they complete. In a second table, known as 'MAYDO', the actions performed by the user are stored (referenced by IDs), and include information such as what the user did (went to a Starbucks, purchased X, did the action between dates Y and Z, etc.)

My Question was, how do I go about referencing each element in my 'MAYDO' table and associate it with the correct user, in the form of a JSON list? I want to have some sort of functionality like this:

{
    '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'
      }]
}

Essentially, I want all the rows in the 'MAYDO' table to associate with their respective Users who performed them, and basically become the list for that User's JSON dataa (one JSON file per person). The code I have currently tested only takes the last entry from the 'MAYDO' table (in the event of multiple events), and so I wanted to get some insight into how to fix this. Any help or tips are appreciated. Thank you!

Current code (Encodes the User data perfectly; only encodes the last 'MAYDO' entry)

<!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条回答 默认 最新

  • dounuo9921 2015-06-17 05:47
    关注

    Try something like this and please refer to this helpful link 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);
    
        ?> 
    
    评论

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面