在PHP中,如何以JSON格式从MySQL返回多个列值?

在PHP中,如何以JSON格式从MySQL返回多个列值?,php,mysql,arrays,json,Php,Mysql,Arrays,Json,我正在尝试返回用户注册的所有课程(课程1、课程2等)的列表。目前,我有以下代码: $mysqli = new mysqli("localhost","username","password","sampleTest"); if (mysqli_connect_errno()) { printf("Can't connect to SQL Server. Error Code %s\n", mysqli_connect_error($mysqli)); exit; } // Set

我正在尝试返回用户注册的所有课程(课程1、课程2等)的列表。目前,我有以下代码:

$mysqli = new mysqli("localhost","username","password","sampleTest");
if (mysqli_connect_errno()) {
    printf("Can't connect to SQL Server. Error Code %s\n", mysqli_connect_error($mysqli));
    exit;
}
// Set the default namespace to utf8
$mysqli->query("SET NAMES 'utf8'");
$json   = array();
if($result = $mysqli->query("select course1 from users where username ='test'")) {
    while ($row=$result->fetch_assoc()) {
        $json[]=array(
            'courses'=>$row['course1'],

        );
    }
}
$result->close();

header("Content-Type: text/json");
echo json_encode(array( 'courses'  =>   $json )); 

$mysqli->close(); 

我可以拿到第一道菜,但其他菜不行。我已经尝试了从username='test'所在的用户中选择*,但是我在传递数组时遇到了困难。

如果您这样做怎么办:

<?php
...
$json = array();
$json['courses'] = array();
if($result = $mysqli->query("select course1 from users where username ='test'")) {
    while ($row=$result->fetch_assoc()) {
        $json['courses'][] = $row['course1'];
    }
}
...
?>

如果只想列出课程,不需要列出所有这些行

while ($row=$result->fetch_assoc()) {
    $json[]=array(
        'courses'=>$row['course1'],

    );
}
这就足够了

while ($row=$result->fetch_assoc()) {
    $json[]= $row['course1'] ;
}
尝试执行以下操作:

$mysqli = new mysqli("localhost","username","password","sampleTest");
if (mysqli_connect_errno()) {
    printf("Can't connect to SQL Server. Error Code %s\n", mysqli_connect_error($mysqli));
    exit;
}
// Set the default namespace to utf8
$mysqli->query("SET NAMES 'utf8'");
$json   = array();
if($result = $mysqli->query("select * from users where username ='test'")) {
    while ($row=$result->fetch_assoc()) {
        $json[]=array(
            'courses'=> array(
                  'cource1' => $row['course1'],
                  'cource2' => $row['course2'],
                  'cource3' => $row['course3'],
            ),
        );
    }
}
$result->close();
header("Content-Type: text/json");
echo json_encode(array( 'courses'  =>   $json )); 
$mysqli->close();
假设您拥有名为“course1”、“course2”、“course3”等字段和“username”的users表。 您可以在sql查询中的SELECT关键字后执行*或仅列出必填字段(逗号分隔)。 例如,您有一个名为test的用户:

username course1 course2 course3
  test     Yes     Yes     No
您将只收到表中的一行。 你会得到

//var_dump($json);

array(1) {
  [0]=>
  array(1) {
    ["cources"]=>
    array(3) {
      ["cource1"]=>
      string(3) "Yes"
      ["cource2"]=>
      string(3) "Yes"
      ["cource3"]=>
      string(2) "No"
    }
  }
}
在执行上述代码之后。 然后,您只需要对这个数组进行json_encode()。
希望能有所帮助。

看来您的数据库设计错误

这些课程应存储在单独的表中,而不是存储在用户表中。
这样就可以很容易地检索到它


为了更明确地回答,需要更多的数据库结构数据。

您希望看到什么以及实际看到什么?不是
$json[]=array('courses'=>$row['course1'],)
导致
$json
数组被覆盖?@Clyde:no,它会将新数组推送到现有的$json[]@Gaurav:thanx,它已经在php中编写了一些有用的代码;)为什么不尝试
echo mysql\u num\u行($result)检查返回的行数?这为我返回了“对象”。我没有提到上面的代码块必须插入到您的代码中。我已经编辑了上面的代码