我从MySQL数据库中获取数据时遇到问题。它获取行,并且当使用print_r打印结果时,它看起来是正确的,但是当在数组中获取一个项目总是为空时,我不知道为什么!
如果我使用phpMyAdmin,我可以看到数据库中的所有行,看起来是正确的。
有人有个主意吗?
<?php
$con = mysqli_connect($host, $user, $password) or die("Failed to connect to MySQL: " . mysqli_connect_error());;
mysqli_select_db($con, $database) or die("Cannot select DB");
$r = mysqli_query($con, "SELECT Question FROM TempQuestions") or die('Query failed: ' . mysql_error());
Print "Rows ". mysqli_num_rows($r) . "<br>"; // Returns 10 rows
while ($dbResult = mysqli_fetch_array( $r)) {
print_r($dbResult); // Prints the question like Array ([0] => The question in DB, [Question] => The question in DB)
Print "<br><br>";
Print "Question: ";
Print $dbresult['Question']; // Is always empty!
Print $dbresult[0]; // Is always empty!
Print "<br><br>";
}
mysqli_free_result($r);
mysqli_close($con);
?>
答案 0 :(得分:3)
... or die('Query failed: ' . mysql_error());
您无法将mysql_*
与mysqli_*
混淆。
现在应该可以了,变量名称区分大小写:
while ($dbresult = mysqli_fetch_array($r)) {
print_r($dbresult);
Print "<br><br>";
Print "Question: ";
Print $dbresult['Question'];
Print $dbresult[0];
Print "<br><br>";
}