-1

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?

user41965
  • 111
  • 1
  • 7
  • what happens if you substitute this line: echo "
  • [" . $row['subject'] . "] " . $row['note'] . " by " . $row['user'] . " on " . $row['dat'] . "
  • "; with echo "test"; ? Do you get test echoed as many times as there are rows? – amanda fouts Aug 02 '14 at 18:46
  • Yes, that code given below is working, but the code given above is not. – user41965 Aug 02 '14 at 18:53
  • Do you mean Manfred's code works? – amanda fouts Aug 02 '14 at 18:56
  • No, Manfred's code is not working the third code block I gave in the question is working, but the first and second block are not working(second block of code is only a small change in first block of code) – user41965 Aug 02 '14 at 21:28
  • also try this code for debugging what is in the $row, etc. variables: var_dump($row); //shows output of variable for debug purposes – amanda fouts Aug 02 '14 at 23:09
  • Printing the value as NULL for var_dump($row); @amandafouts – user41965 Aug 02 '14 at 23:41
  • do a dump of this var. as well: $htno = $_POST['htno']; as in var_dump($htno) to see if the post variable is pulling a null or invalid value. If this is not the issue, refer to this page: http://php.net/manual/en/mysqli.prepare.php and prepared statement example. – amanda fouts Aug 03 '14 at 03:14
  • @amandafouts the post variable contains the correct variable, even the ` printf("Number of rows: %d.\n", mysqli_stmt_num_rows($stmt));` is also printing the correct no. of rows, only(according to what I observed) only the statement `$row = mysqli_fetch_array($result)` is not working and therefore the control is not going inside the while loop. Just can't understand why it's not working, can anyone please try the code – user41965 Aug 08 '14 at 16:24