简体   繁体   中英

Mysql - select several rows with same value

I've got a table called 'data', and some columns:
user,unit,price,discount,description.

 -----   -----  ------  ---------   ----------- 
| 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);
 }

From this code, I get:

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

so it's just the first row. I want to get both of them like this:

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

so there will be 2 arrays.

You will need to write a loop containing calls of mysqli_fetch_assoc() . As you iterate, store the rows of data.

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

Or if you replace the * in your SELECT clause to nominate your desired rows, you can use the top-voted comment on the mysqli_fetch_assoc() manual page...

for ($result = []; $row = mysqli_fetch_assoc($r); $result[] = $row);

This compact one-liner will convert your resultset into a multidimensional array with the same structure as the previous code block without the iterated array_push() function calls.

<?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);
 }

Warning : code is vulnerable to SQL injection attacks

first of all your query is open to sql injection you should use mysqli preprared statement

so then your code would look something like this

Edit: my original answer was incomplete as i didn't test it below is my corrected answer

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM