Php Mysql-选择具有相同值的多行
我有一个名为“data”的表和一些列:Php Mysql-选择具有相同值的多行,php,mysql,select,rows,Php,Mysql,Select,Rows,我有一个名为“data”的表和一些列: 用户、单位、价格、折扣、说明 ----- ----- ------ --------- ----------- | user | unit | price | discount | description | ----- ----- ------ --------- ----------- | test | unit | 100 | 50 | des | ----- ----- ---
用户、单位、价格、折扣、说明
----- ----- ------ --------- -----------
| user | unit | price | discount | description |
----- ----- ------ --------- -----------
| test | unit | 100 | 50 | des |
----- ----- ------- -------- -----------
| test2| unit2 | 200 | 20 | des |
----- ----- ----- -------- -----------
所以这只是第一排。我想让他们两个都这样:
{"result":[{"user":"test","unit":"unit","price":"100","discount":"50"}]}
{"result2":[{"user":"test2","unit":"unit2","price":"200","discount":"20"}]}
因此将有2个数组。您需要编写一个包含调用
mysqli\u fetch\u assoc()
的循环。迭代时,存储数据行
while($row = mysqli_fetch_assoc($r)){
array_push($result, array(
"user" => $row['user'],
"unit" => $row['unit'],
"price" => $row['price'],
"discount" => $row['discount']
)
);
}
或者,如果替换选择
子句中的*
来指定所需的行,则可以使用mysqli\u fetch\u assoc()
手动页面上投票最多的注释
for ($result = []; $row = mysqli_fetch_assoc($r); $result[] = $row);
这个紧凑的单行程序将把您的结果集转换成一个多维数组,该数组的结构与前面的代码块相同,而不需要迭代的
array\u push()
函数调用。首先,您的查询是开放的,可以使用sql注入
<?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);
}
那么你的代码看起来是这样的
编辑:我的原始答案不完整,因为我没有测试。下面是我的更正答案
<?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();
警告:mysqli_fetch_assoc()期望参数1是mysqli_result,在第9行{“result”:[]}@NikitaIvanov I修复了它致命错误:未捕获错误:在/storage/h7/538/1215538/public_html/UserLogin.php中调用布尔值上的成员函数fetch_array()在第11行输入/storage/h7/538/1215538/public_html/UserLogin.php。我已经更新了我的答案,很抱歉我最初发布了错误警告:您的代码易受攻击。请阅读以了解更多关于如何预防它的信息。@Pang我们谈论的是获取所有记录,而不是如何处理SQL注入,这是她的工作,显然她在这方面是新手,所以不要把事情复杂化。
<?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);
}
<?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();