-1
$retval = mysqli_query($conn,"SELECT * FROM food WHERE pnrno='$pnrno'");
if ($retval) {
    echo "Your food complain has been successfully fetched";
    echo "<table border='1'>
        <tr>
        <th>Username</th>
        <th>PNR Number</th>
        <th>Food Complain Status</th>
        </tr>";

        while($row = mysqli_fetch_array($retval))
        {
        echo "<tr>";
        echo "<td>" . $row['username'] . "</td>";
        echo "<td>" . $row['pnrno'] . "</td>";
        echo "<td>" . $row['complain_status'] . "</td>";
        echo "</tr>";
        }
        echo "</table>";
        echo "\r\n";
} else {
    echo "Error: " . $retval . "<br>" . mysqli_error($conn);
}

Code inside the while loop is not getting executed(I think so), "Your food complain has been successfully fetched" this message is getting printed, table is formed, but username, pnrno, and complain_status after being fetched from database is not printed on webpage. Why is it so, please help.

Ayushi Singh
  • 19
  • 1
  • 3
  • 1
    Try adding a var_dump($row) inside the while and see if it says anything interesting – Phiter Jul 13 '17 at 18:04
  • 2
    [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)*** Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Jul 13 '17 at 18:04
  • Add [error reporting](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php/845025#845025) to the top of your file(s) _while testing_ right after your opening PHP tag for example ` – RiggsFolly Jul 13 '17 at 18:05
  • You need to use `mysqli_fetch_assoc` instead of `mysqli_fetch_array` – GrumpyCrouton Jul 13 '17 at 18:07
  • @GrumpyCrouton The default for `mysqli_fetch_array()` is `MYSQLI_BOTH` so `mysqli_fetch_assoc()` would be tidier, but not necessary – RiggsFolly Jul 13 '17 at 18:11
  • @RiggsFolly You're right, I've just not seen an instance where the other way is better – GrumpyCrouton Jul 13 '17 at 18:17
  • 1
    why don't you try to simply debug it first, what's the point to run to SOF with every tiniest scratch? – Yuri G Jul 13 '17 at 18:17

2 Answers2

3

Assuming the data exists, and you're sending the query the correct parameter, the likely culprit is how you're using your row variable. Since you're not telling mysqli to fetch the array in an associative manner, it's defaulting to an index.

You need to change your fetch function to:

mysqli_fetch_array($retval, MYSQLI_ASSOC)
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Jacobm001
  • 4,431
  • 4
  • 30
  • 51
0
$retval = mysqli_query($conn,"SELECT * FROM food WHERE pnrno='$pnrno'");

if ($retval) {

Your if is going to execute if the query successfully executes.

However, a query for a non-existent row will succeed, with zero rows.

You'll want to check the number of rows returned by the query, and show an error if there aren't any matching ones.

ceejayoz
  • 176,543
  • 40
  • 303
  • 368