I tried to execute the following php program in my friend's laptop, but the nothing is getting executed inside the while loop.
<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>
So, why is the code inside the loop not getting executed while all the code outside the while loop (also which prints the no. of rows in the result) is executing properly, I've searched in Google but and tried solutions but nothing is getting displayed if I use
while($row = mysqli_fetch_array($stmt, MYSQLI_ASSOC)) or
while($row = mysqli_fetch_array($stmt, MYSQLI_NUM)) //with $row[0]...
I tried a similar code which is working, in case if it helps:
$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);
To check if it was problem with the computer I tried the same code in other computer as well, but it wasn't executing, don't know why some code is executing and some are not, even when I'm not able to fetch the result but the function printf("Number of rows: %d.\n", mysqli_stmt_num_rows($stmt));
is giving the correct output, with correct no. of rows.
I'm running ubuntu 14.04 I also tried the code on my friend's laptop who is running unbuntu 14.04, can anyone please check both the codes if they are running, still can't understand why only that code is not working, both the codes(that work and that don't work) look same to me, but don't know why when I write code for different purposes, sometimes they work and sometimes they don't.
Can anyone please try the code?