2011-02-25 144 views
0

我試圖返回所有用戶在(course1,course2等)目前,我有以下的代碼被登記的課程的列表:在PHP中,如何以JSON格式返回MySQL中的多個列值?

$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(); 

我能拿到第一課程顯示,但不是其他人。我試過select * from users where username ='test',但是我被卡在傳遞數組上。

+0

你期望看到什麼以及你看到的是什麼? – galymzhan 2011-02-25 06:17:08

+0

isnt $ json [] = array( 'courses'=> $ row ['course1'], ); '導致'$ json'數組被覆蓋? – 2011-02-25 06:20:25

+1

@Clyde:不,它會將新數組推到現有的$ json [] – Gaurav 2011-02-25 06:23:14

回答

0

如果你正在做類似的東西是什麼:

<?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']; 
    } 
} 
... 
?> 
0

如果你想只列出的課程,你並不需要所有這些行。

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

    ); 
} 

這將是足夠的

while ($row=$result->fetch_assoc()) { 
    $json[]= $row['course1'] ; 
} 
0

嘗試執行此:

$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 '等等和'用戶名'。 您可以在sql查詢中的SELECT關鍵字後執行*或僅列出必填字段(以逗號分隔)。 例如,你有名字測試一個用戶:

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()這個數組。 希望它會有所幫助。

+0

這爲我返回「對象對象」。不是作爲數組的課程 – Joe 2011-02-26 04:54:59

+0

我沒有提到上面的代碼塊必須插入你的代碼中。我已經編輯了我的代碼 – olezhek 2011-03-29 09:11:31

2

看來你的數據庫有錯誤的設計。

這些課程應存儲在單獨的表格中,而不是存儲在用戶表格中。
然後它可以很容易地檢索。

爲了更確切地回答,需要更多的數據庫結構數據。

+0

+1「select course1 from users」已經將其提供。 – 2011-02-26 23:50:25