我正在尝试从 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/