2017-04-01 56 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

相关问题