gpt4 book ai didi

php - 选定的 mysqli 行

转载 作者:行者123 更新时间:2023-11-30 22:51:04 24 4
gpt4 key购买 nike

我在这里尝试做的是,如果我的查询中有选定的行,则返回一条 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");
}
}
?>

最佳答案

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

关于php - 选定的 mysqli 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28145193/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com