Data not displaying from database - error?

I am attempting to use this code:

[code]<?php

$sql = “SELECT name, score FROM scoreboard”;
$result = mysql_query($sql);

if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo “Name:” . $row[‘name’] . " - Score: " . $row[‘score’] . “
”;
}
} else {
echo “No results!”;
}
?>
[/code]

There are about 70 items in the database but when I use that code, it displays zero results. Any idea what would cause that?

Has your host updated to a version of PHP that no longer supports the deprecated mysql_ functions?

I agree that if you are just learning, go with a modern connection like PDO.

To debug what you have put this at top of page.

error_reporting( E_ALL );

Do you have DB connect for mysql?

//MySQL Database user name.
$login = "";
//Password for MySQL.
$dbpass = "";
//MySQL Database name.
$db = "";
//Host - usually localhost 
$host = "localhost";

//Make connection to DB
mysql_connect("$host","$login","$dbpass") OR DIE
        ("There is a problem with the system.  Please notify your system administrator." .mysql_error());

mysql_select_db("$db") OR DIE
        ("There is a problem with the system.  Please notify your system administrator." .mysql_error());

Does it display zero results or does it display the message “no results” ?

By the way, mysql_query doesn’t return an object. Are you sure you’re not using mysqli_ instead?

Try this:

If ($result->num_rows() > 0) }

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.