我有这个查询在同一个网站的其他部分工作正常,但我遇到的问题是检查说不是真的,但它实际上是否正确执行?
<?php
$conn = new mysqli($mysql_host, $mysql_user, $mysql_password, $mysql_database);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM Stock WHERE StockItemID='12'";
$result = $conn->query($sql) or die( $conn->connect_error );
$row = $result->fetch_assoc();
if ($conn->query($sql) === TRUE) {
echo $row['StockCategory'];
} else {
printf("Errorcode: %d\n", $conn->errno);
}
$conn->close();
?>
因为在上面的例子中我没有得到回声,我认为它没有用但我回来了 - 错误代码:0
但是,如果我从表中回显出一个值,它就会起作用。
答案 0 :(得分:0)
选择查询返回结果集
$conn = new mysqli($mysql_host, $mysql_user, $mysql_password, $mysql_database);
if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); }
$sql = "SELECT * FROM Stock WHERE StockItemID='12'";
$result = $conn->query($sql) or die( $conn->connect_error );
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
echo $row['StockCategory'];
} else {
printf("Errorcode: %d\n", $conn->errno);
}
$conn->close();
答案 1 :(得分:0)
如果您想查看查询是否返回一行,那么检查该行本身。
是的,尽可能简单和合乎逻辑。
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = new mysqli($mysql_host, $mysql_user, $mysql_password, $mysql_database);
$sql = "SELECT * FROM Stock WHERE StockItemID='12'";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
if ($row) { // here
echo $row['StockCategory'];
}