我在这里尝试做的是返回一条json_encode消息,如果在我的查询中选择了行。我试图使用num_rows()或mysqli_num_rows()但它也无法正常工作。我想要回归"失败"控制台消息,如果没有选定的行。如果我的查询中选择了一条记录,那么我的数组也是如此。有什么帮助吗?
<?php
if(isset($_POST['code'])) {
include('connect.php');
$code= $mysqli->real_escape_string($_POST['code']);
$sql = $mysqli->query("SELECT * FROM list WHERE pr='$code'");
while($row = $sql->fetch_assoc())
{
$pr= $row['pr'];
$approved= $row['approved'];
$requested = $row['requested'];
}
if($row->affected_rows >= 1) {
echo json_encode(array("pr" => $pr,"approved" => $approved, "requested" => $requested));
} else {
echo json_encode("failed");
}
}
?>
答案 0 :(得分:0)
<?php
$userInput = $_POST['code'];
if(isset($userInput)) {
require('connect.php');
$code = $mysqli->real_escape_string($userInput);
$sql = $mysqli->query("SELECT * FROM list WHERE pr='".$code."'");
if(!empty($sql)) {
while($row = $sql->fetch_assoc())
{
$pr = $row['pr'];
$approved = $row['approved'];
$requested = $row['requested'];
}
echo json_encode(["pr" => $pr, "approved" => $approved, "requested" => $requested]);
}
else echo json_encode("failed");
}