[英]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. 所以会有2个数组
You will need to write a loop containing calls of mysqli_fetch_assoc()
. 您将需要编写一个包含
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... 或者,如果在
SELECT
子句中替换*
以指定所需的行,则可以在mysqli_fetch_assoc()
手册页上使用投票最多的注释...
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. 这种紧凑的单行代码可将您的结果集转换为具有与先前代码块相同结构的多维数组,而无需迭代
array_push()
函数调用。
<?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 警告 :代码容易受到SQL注入攻击
first of all your query is open to sql injection you should use mysqli preprared statement 首先,您的查询对SQL注入开放,您应该使用mysqli preprared语句
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.