我试着在朋友的笔记本电脑上执行以下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的朋友的笔记本电脑上的代码,任何人都可以检查这两个代码是否正在运行,仍然无法理解为什么只有那些代码不起作用,两者代码(工作和不工作)看起来和我一样,但不知道为什么当我为不同的目的编写代码时,有时它们会工作,有时它们却不工作。
任何人都可以试试代码吗?
答案 0 :(得分:1)
您的$stmt
类型为 mysqli_stmt ,这就是为什么它适用于mysqli_stmt_num_rows
mysqli_fetch_array但是需要一个 mysqli_result ,你可以通过存储mysqli_stmt_store_result($stmt);
答案 1 :(得分:-3)
使等号加倍,如下:
while($row == mysqli_fetch_array($stmt))