gpt4 book ai didi

php - 判断 PHP mysqli fetch 是否返回数据

转载 作者:行者123 更新时间:2023-11-29 07:49:28 24 4
gpt4 key购买 nike

我正在尝试从 PHP 网站获取以下代码,并重写以在返回的 SQL 数据为空时进行回显。

<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

$city = "Amersfoort";

/* create a prepared statement */
if ($stmt = $mysqli->prepare("SELECT District FROM City WHERE Name=?")) {

/* bind parameters for markers */
$stmt->bind_param("s", $city);

/* execute query */
$stmt->execute();

/* bind result variables */
$stmt->bind_result($district);

/* fetch value */
$stmt->fetch();

printf("%s is in district %s\n", $city, $district);

/* close statement */
$stmt->close();
}

/* close connection */
$mysqli->close();
?>

我在确定执行此操作的正确条件时遇到问题。只是寻找有关如何进一步优化此代码的想法。

最佳答案

$rows = $stmt->rowCount();
echo $rows;

关于php - 判断 PHP mysqli fetch 是否返回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26852399/

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