gpt4 book ai didi

php - 查询数据在MySQL本地显示,但在PHP中查询为空

转载 作者:行者123 更新时间:2023-11-29 01:55:40 25 4
gpt4 key购买 nike

我有一个简单的数据库,用于每隔 10 到 30 分钟存储一次泳池温度和室外温度。我遇到的问题是我拥有的代码我可以在 MySQL 中本地运行查询并且它工作得很好。但是当我在 PHP 中有相同的查询时,它会给我一个空白页。

<?php
$dbhost = "localhost";
$dbuser = "xxxx";
$dbpass = "xxxxxxxxxx";
$dbname = "pool_db";

// Create connection
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname) or die("Error " .mysqli_error($conn));


$sql = "SELECT * FROM tempLog ORDER BY dateTime DESC";
$results = mysqli_query($conn, $sql) or die("Error " .mysqli_error());
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
while($row = mysqli_fetch_array($results)); {
?>
<p><?php echo $row['poolTemp'];?></p>
<?php
}
?>
</body>
</html>

最佳答案

就是这一行,分号:

while($row = mysqli_fetch_array($results)); {
^ end of statement character

它就在那里杀死它,然后静静地失败,反过来给你一个空白的屏幕; 删除它

  • 这是一个不会抛出错误的有效字符。因此使用错误报告是徒劳的。

As in C or Perl, PHP requires instructions to be terminated with a semicolon at the end of each statement. The closing tag of a block of PHP code automatically implies a semicolon; you do not need to have a semicolon terminating the last line of a PHP block. The closing tag for the block will include the immediately trailing newline if one is present.

  • 此处的关键字是“已终止”。

  • 还要确保您确实为 $row['poolTemp'] 设置了一个名为“poolTemp”的行。

我自己的测试:

[黑屏] - 带分号

<?php 

$link = mysqli_connect('xxx','xxx','xxx', 'xxx');
if (!$link) {
die('Could not connect to MySQL: ' . mysqli_error());
}
echo 'Connection OK';

echo "<hr>";

$sql = mysqli_query($link,"SELECT * FROM table");

while($row = mysqli_fetch_assoc($sql));{
// ^ with semi-colon

echo $row['col1'] . " " . $row['col2'] . "<br>";

}

mysqli_close($link);

?>

[结果成功显示] - 没有分号

<?php 

$link = mysqli_connect('xxx','xxx','xxx', 'xxx');
if (!$link) {
die('Could not connect to MySQL: ' . mysqli_error());
}
echo 'Connection OK';

echo "<hr>";

$sql = mysqli_query($link,"SELECT * FROM table");

while($row = mysqli_fetch_assoc($sql)) {
// ^ no semi-colon

echo $row['col1'] . " " . $row['col2'] . "<br>";

}

mysqli_close($link);

?>

关于php - 查询数据在MySQL本地显示,但在PHP中查询为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29958361/

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