2

I am currently working a tutorial on php and mysql.I am currently working on a script which should read data from sql database and show that data in html table.I have a problem where the table is generated with empty fields. It has correct number of fields but those fields are empty.

Here is my php script:

<?php
require "init.php";

$select = "select * from ScoresTest";
$sqlSel = mysqli_query($con,$select);
if(mysqli_num_rows($sqlSel)>0)
{

    echo("Works");
}
echo"<table border = '1'>
<tr>
<th>Username</th>
<th>Score</th>
</tr>";
while($row  = mysqli_fetch_row($sqlSel))
{

    echo "<tr>";
  echo "<td>" . $row['USERNAME'] . "</td>";
  echo "<td>" . $row['SCORES'] . "</td>";
  echo "</tr>";
}

echo"</table>";


?>

Here is how the result looks like: Html table

Number of rows in my table is ok but the fields are empty.Why?

10
  • 1
    mysqli_error($con) shows you what? undefined indexes maybe? Commented Mar 27, 2018 at 14:29
  • Try printing $row inside the while loop, you will get the keys. Commented Mar 27, 2018 at 14:30
  • @Nagesh they're already doing that. Commented Mar 27, 2018 at 14:31
  • you're using the wrong function/method of retrieval here. RTM. Commented Mar 27, 2018 at 14:32
  • @FunkFortyNiner I tried mysqli_error($con), it shows nohing Commented Mar 27, 2018 at 14:34

3 Answers 3

1

Another way could be changing mysqli_fetch_row to mysqli_fetch_assoc

while($row  = mysqli_fetch_assoc($sqlSel))
{

    echo "<tr>";
    echo "<td>" . $row['USERNAME'] . "</td>";
    echo "<td>" . $row['SCORES'] . "</td>";
    echo "</tr>";
}
Sign up to request clarification or add additional context in comments.

Comments

0

The manual on fetch_row() https://secure.php.net/manual/en/mysqli-result.fetch-row.php

shows using the following array keys $row[0], $row[1].

But you're using the column names.

In that case, use fetch_assoc() https://secure.php.net/manual/en/mysqli-result.fetch-assoc.php

Plus, make sure those are the actual names and not in another letter case.

PHP's error reporting will (also) help you here https://php.net/manual/en/function.error-reporting.php

NOTE: On *NIX, VAR and var are two different animals.

You also need to run this off a webserver and using the correct protocol.

That's unknownst to us.

2 Comments

Thank you! I changed to $row[0], $row[1] and it displays the data!
@CreationApps I did make an edit earlier about using fetch_assoc() if you wish to continue using the named array keys.
0

In this code,

while($row  = mysqli_fetch_row($sqlSel))
{

  echo "<tr>";
  echo "<td>" . $row['USERNAME'] . "</td>";
  echo "<td>" . $row['SCORES'] . "</td>";
  echo "</tr>";
}

Sure if you name of row is $row['USERNAME']? maybe the name of you row is in lowercase or the name of you fields is not correct. Also you can use the position of the row "for debug"( $row[0], $row[1])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.