2017-04-01 68 views
0

我有一張名爲'data'的表,並且有一些列:
用戶,單位,價格,折扣,說明。Mysql - 選擇具有相同值的多行

----- ----- ------ --------- ----------- 
| user | unit | price | discount | description | 
----- ----- ------ --------- ----------- 
| test | unit | 100 | 50 |  des  | 
----- ----- ------- -------- ----------- 
| test2| unit2 | 200 | 20 |  des  | 
----- ----- ----- -------- ----------- 
<?php 
if($_SERVER['REQUEST_METHOD']=='GET'){ 
$id = $_GET['id']; 
require_once('dbConnect.php'); 
$sql = "SELECT * FROM data WHERE description='".$id."'"; 
$r = mysqli_query($con,$sql); 
$res = mysqli_fetch_array($r); 
$result = array(); 
array_push($result,array(
    "user"=>$res['user'], 
    "unit"=>$res['unit'], 
    "price"=>$res['price'], 
    "discount"=>$res['discount'] 
) 
); 
echo json_encode(array("result"=>$result)); 
mysqli_close($con); 
} 

從這個代碼,我得到:

{"result":[{"user":"test","unit":"unit","price":"100","discount":"50"}]} 

所以它只是第一行。我想得到他們這樣的:

{"result":[{"user":"test","unit":"unit","price":"100","discount":"50"}]} 
{"result2":[{"user":"test2","unit":"unit2","price":"200","discount":"20"}]} 

所以會有2個數組。

+0

的[獲取與mysqli的結果行的陣列]可能的複製(http://stackoverflow.com/questions/1501274/get-array- of-rows-with-mysqli-result) – mickmackusa

+0

我看到你的問題已經被低估了。一個低調的問題意味着你的問題顯示缺乏研究,不清楚和/或無益。因爲這個問題已經被php.net解答了,並且在SO上被多次詢問和回答,您可以期待收到更多的優惠。請閱讀關於如何提出一個好問題的教程。總是詳盡地研究你的問題,並試圖在未來發布問題之前自我解決。這個問題對未來的SO讀者不會有用,除非您先刪除它(首選),否則可能會被版主關閉。 – mickmackusa

+0

警告:您的代碼容易受到[SQL注入攻擊](https://en.wikipedia.org/wiki/SQL_injection)的影響。請閱讀[本文](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)瞭解更多關於如何防止它。 – Pang

回答

0
while($row = mysqli_fetch_assoc($r)){ 
    array_push($result,array(
     "user"=>$row['user'], 
     "unit"=>$row['unit'], 
     "price"=>$row['price'], 
     "discount"=>$row['discount'] 
     ) 
    ); 
} 
+1

警告:mysqli_fetch_assoc()期望參數1是mysqli_result,在第9行的/storage/h7/538/1215538/public_html/UserLogin.php中給出的數組 {「result」:[]} –

+0

@NikitaIvanov我修復了它 – mickmackusa

0
<?php 
if($_SERVER['REQUEST_METHOD']=='GET'){ 
$id = $_GET['id']; 
require_once('dbConnect.php'); 
$sql = "SELECT * FROM data WHERE description='".$id."'"; 
$r = mysqli_query($con,$sql); 
$result = array(); 
while ($res = mysqli_fetch_assoc($r)){ 
    $aRaw["user"] = $res['user']; 
    $aRaw["unit"] = $res['unit']; 
    $aRaw["price"] = $res['price']; 
    $aRaw["discount"] = $res['discount']; 
    $result[] = $aRaw; 
} 
); 
echo json_encode(array("result"=>$result)); 
mysqli_close($con); 
} 

警告:代碼容易受到SQL注入式攻擊

+0

@ Pang我們談論關於獲取所有記錄而不是關於如何處理SQL注入的問題,並且與她一起工作,顯然她是新的,所以不要讓事情變得複雜。 – mohe14

+0

@Pang可以重新提出答案。謝謝。 – mohe14

0

首先查詢的是開放的SQL注入你應該使用mysqli preprared statement

這樣的話你的代碼看起來像這樣

編輯:我原來的回答呃是不完整的,因爲我沒有測試它下面是我的修正答案

<?php 
if($_SERVER['REQUEST_METHOD']=='GET'){ 
$id = $_GET['id']; 
require_once('dbConnect.php'); 
$sql = "SELECT * FROM data WHERE description=?"; 
$stmt = mysqli_prepare($con,$sql); 
$stmt->bind_param("s", $id); 
$stmt->execute(); //originally I combined this and next line, it was incorrect 
if ($result = $stmt->get_result()){ 
    //originally I used wrong method below 
    while($row = $result->fetch_assoc()) { 
      $myArray[] = $row; 
    } 
    //uncomment below if you're sending response as json responese 
    //header('Content-Type: application/json'); 
    echo json_encode($myArray); 
} 

$result->close(); 
+0

致命錯誤:未捕獲錯誤:調用成員函數fetch_array()布爾/ in /storage/h7/538/1215538/public_html/UserLogin.php:11堆棧跟蹤:#0 {main}拋出/ storage/h7/538 /1215538/public_html/UserLogin.php 11行 –

+0

我已經更新了我的答案,我很抱歉,我原來錯誤地發佈了它 – Jpsh

相關問題