Daniel Juhl Sørensen Daniel Juhl Sørensen - 5 months ago 7x
PHP Question

Mysqli result always returns 0

Hi I can connect to my database but it's says that I have 0 rows!
I've tried different ways to connect to my database, but all of them always return 0 rows so it will not show any data from the database.

$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);

$sql = "SELECT * FROM keys";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Key: " . $row["key"]. " " . $row["used"]. "<br>";
} else {
echo "0 results";


KEYS is a reserved term, hence your query fails to execute and no data is returned, at the very least escape it.

Best option would be to change your field name to something that's not reserved.

Something funny to note

You're saying if there are some rows in my result show me those but if there are no rows in my result then show me the number of rows. In short, displaying the number of rows when your query fails is utterly pointless! That will be a good place to display an error message returned by the database driver