我准备了两条在一个 sql 连接上运行的 sql 语句。当我在 phpMyAdmin 中运行它们时 - 第一个返回一行,第二个返回几行。但是,当我在 php 中使用以下代码时:
$conn = new mysqli($servername, $username, $password, $dbname);
(...)
$sql1 = "SELECT ...";
$sql2 = "SELECT ...";
$result = $conn->query($sql1);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "INSIDE IF:: " . $row["end_time"];
$variable = $row["end_time"];
}
}
else{
echo "FIRST ELSE";
$variable = $sth;
}
$result2 = $conn->query($sql2);
if ($result2->num_rows > 0) {
echo "the 2nd statement worked"; (...)
}
我在屏幕上看到消息:FIRST ELSE 并且第二条语句有效。为什么我看不到“INSIDE IF”?我 100% 肯定第一个查询返回一行。
最佳答案
试试这个:
if ($result = $conn->query($sql1)) {
while ($row = $result->fetch_assoc()) {
echo "INSIDE IF:: " . $row["end_time"];
$variable = $row["end_time"];
}
$result->free();
} else {
echo "FIRST ELSE";
}
关于php - 我无法在 php 中获取来自 mysql 数据库的一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29592198/