我尝试在我朋友的笔记本电脑上执行以下 php 程序,但 while 循环内没有执行任何内容。
<html>
<body>
<?php
$db_hostname = "localhost";
$db_username = "user";
$db_password = "password";
$db_name = "resulta";
$link=mysqli_connect($db_hostname,$db_username,$db_password,$db_name);
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$htno = $_POST['htno'];
$query="select * from marks where hallno=?";
if ($stmt = mysqli_prepare($link, $query)) {
mysqli_stmt_bind_param($stmt, 's', $htno);
/*http://php.net/manual/en/mysqli-stmt.bind-param.php*/
/* execute query */
mysqli_stmt_execute($stmt);
/* store result */
mysqli_stmt_store_result($stmt);
printf("Number of rows: %d.\n", mysqli_stmt_num_rows($stmt));
if(mysqli_stmt_num_rows($stmt)>0)
{
echo "thank you";
echo "recieved";
echo "<table>";
echo "<tr><th>Hall Ticket No.</th><th>Sub. Code</th><th>Sub. Name</th><th>Internal Marks</th><th>External Marks</th><th>Total Marks</th><th>Credits</th></tr>";
//$stmt = mysqli_query($link,$query);
while($row = mysqli_fetch_array($stmt)) {
echo "inside yeah!!!";
echo "<tr><td>" . $row['hallno'] . "</td><td>" . $row['subcode'] . "</td><td>" . $row['subname'] . "</td><td>" . $row['intemarks'] . "</td><td>" . $row['extmarks'] . "</td><td>" . $row['totalmarks'] . "</td><td>" . $row['credits'] . "</td></tr>";
}
echo "</table>";
echo "over";
mysqli_stmt_close($stmt);
mysqli_close($link);
}
else
{
/* close statement */
mysqli_stmt_close($stmt);
echo "Roll No. not found";
}
}
?>
</body>
</html>
那么,为什么循环内的代码没有被执行,而 while 循环外的所有代码(也打印结果中的行数)都正确执行,我在 Google 中搜索过,但尝试了解决方案,但没有任何结果如果我使用
就会显示while($row = mysqli_fetch_array($stmt, MYSQLI_ASSOC)) or
while($row = mysqli_fetch_array($stmt, MYSQLI_NUM)) //with $row[0]...
我尝试了类似的代码,它可以工作,以防有帮助:
$con=mysqli_connect($db_hostname,$db_username,$db_password,$db_name);
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// select everything from the news table
$st=0;
$result = 'False';
$result = mysqli_query($con,"SELECT * FROM notif ORDER BY dat desc LIMIT $st, 6");
echo "<ul>";
while($row = mysqli_fetch_array($result)) {
echo "<li style='font-family: 'Open Sans',sans-serif;' ><b><u>[" . $row['subject'] . "]</u></b> " . $row['note'] . " <i>by <b>" . $row['user'] . "</b> on <b>" . $row['dat'] . " </b></i></li>";
echo "<br>";
}
echo "</ul>";
// disconnect from the database
mysqli_close($con);
为了检查计算机是否有问题,我也在其他计算机上尝试了相同的代码,但它没有执行,不知道为什么有些代码正在执行,有些则没有,即使我无法执行获取结果,但函数
printf("Number of rows: %d.\n", mysqli_stmt_num_rows($stmt));
给出了正确的输出,并且编号正确。行数。
我正在运行 ubuntu 14.04 我还在运行 unbuntu 14.04 的朋友的笔记本电脑上尝试了代码,任何人都可以检查这两个代码是否正在运行,仍然无法理解为什么只有该代码不起作用,这两个代码(有效和无效)对我来说看起来是一样的,但不知道为什么当我为不同目的编写代码时,有时它们有效,有时则无效。
任何人都可以尝试一下代码吗?
您的
$stmt
属于 mysqli_stmt 类型,这就是它与 mysqli_stmt_num_rows 一起使用的原因
mysqli_fetch_array 但是需要一个 mysqli_result,您可以通过存储
mysqli_stmt_store_result($stmt);
的结果来获得它
将等号加倍,如下所示:
while($row == mysqli_fetch_array($stmt))