CreationApps
CreationApps

Reputation: 23

php display mysql table in html

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?

Upvotes: 2

Views: 562

Answers (3)

Vallie
Vallie

Reputation: 749

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>";
}

Upvotes: 1

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

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.

Upvotes: 0

Marcos Riveros
Marcos Riveros

Reputation: 544

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])

Upvotes: 0

Related Questions