Search code examples
phpmysqlmysqlimysql-error-1064

The total number of mysql results not displaying properly


I am working on a total page view system and each time someone visits a page it adds the current data and their ip address. When I go to display the total number of rows on a particular day the site just displays 'Rows' without the number. Code:

<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";
$date = date("Y-m-d-h-m-s");
$ip = $_SERVER['REMOTE_ADDR'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 
$date = date("Y-m-d");
$result = mysqli_query("SELECT FROM page_views WHERE date = $date");
$num_rows = mysqli_num_rows($result);

echo "$num_rows Rows\n";

$conn->close();
?>

Could someone please indicate what is going wrong.


Solution

  • Your query is failing but you don't know it because you don't check for errors. If you did you would see a syntax error due to you omitting the * from your SELECT clause and your date not being in quotes.

    Change:

    $result = mysqli_query("SELECT * FROM page_views WHERE date = $date");
    

    to:

    $result = mysqli_query("SELECT * FROM page_views WHERE date = '$date'");