【发布时间】:2014-08-02 14:35:00
【问题描述】:
我试图在我朋友的笔记本电脑上执行以下 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 的朋友的笔记本电脑上尝试了代码,任何人都可以检查两个代码是否正在运行,仍然无法理解为什么只有那个代码不起作用,两者代码(有效的和无效的)对我来说看起来一样,但不知道为什么当我为不同目的编写代码时,有时它们有效,有时它们不。
有人可以试试这个代码吗?
【问题讨论】:
-
如果你替换这一行会发生什么: echo "
- [" . $row['主题'] 。 “]”。 $row['note'] 。 “由”。 $row['user'] 。 " 关于 " 。 $row['dat'] 。 “
”;用回声“测试”; ?测试回显的次数是否与行数一样多? -
是的,下面给出的代码可以工作,但上面给出的代码不行。
-
你的意思是曼弗雷德的代码有效吗?
-
不,Manfred 的代码不工作 我在问题中给出的第三个代码块工作,但第一和第二个代码块不工作(第二个代码块只是第一个代码块的一个小改动代码)
-
也可以试试这个代码来调试 $row 等变量中的内容:var_dump($row); //显示用于调试目的的变量输出
标签: php html mysql mysqli web-technologies