I'm trying to select some simple information from a database and display it on a webpage. I can't work out for the life of me why if ($result->num_rows > 0) from the code below evaluates to false. Most of this is copied directly from the W3Schools site from: http://www.w3schools.com/php/php_mysql_select.asp So it really shouldn't have anything wrong with it..
<html>
<div class="panel">
<!--this should print out atleast something from the database, but it's printing 0 results. -->
<?php include("populate_feature_list.php"); ?>
</div>
</html>
This is the contents of the included PHP file:
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = mysql_query($conn,"SELECT name, rating FROM table ORDER BY rating DESC LIMIT 5;");
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Provider: " . $row["name"]. " - Rating: " . $row["rating"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
This is what returns on the SQL server when executing the query:
+----------------------+--------+
| name | rating |
+----------------------+--------+
| persona | 4.8000 |
| personb | 4.7500 |
| personc | 4.6500 |
| persond | 4.1500 |
| persone | 2.4000 |
+----------------------+--------+
And description of table:
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| name | varchar(20) | YES | UNI | NULL | |
| rating | float(8,4) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+