user3599221
user3599221

Reputation: 39

Mysql Fetch not working

i really dont know why this code isnt working.. database connection works, the timestamp is written to the database.

But i cant figure out why i get a blank page with this code here (i should see the timestamp as echo).

Anyone an idea about this ?

Thank you!

<?php
$user = "daycounter";
$password = "1234";
$database = "daycounter";
$host = "localhost";
$date = time();


// Create connection
$conn = new mysqli($host, $user, $password, $database);
// Check connection
if ($conn->connect_error) {
    die("Error: " . $conn->connect_error);
} 
//Insert timestamp in database
$sql = "INSERT INTO datum (datum)
VALUES ('".$date."')";
//check if that worked
if ($conn->query($sql) === TRUE) {
    echo "That worked!";
} 
//get timestamp from db and display it as echo
$select = "SELECT 'datum' FROM 'daycounter'";
$result = mysql_query($select);
while($row = mysql_fetch_object($result))
   {
   echo "$row->datum";

   }

?>

Upvotes: 0

Views: 1578

Answers (2)

Gagan Upadhyay
Gagan Upadhyay

Reputation: 255

$select = "SELECT 'datum' FROM 'daycounter'";
$result = mysqli_query($conn, $select);

while($row = mysqli_fetch_object($result)) {
    echo "$row->datum";
}

Upvotes: 0

Marc B
Marc B

Reputation: 360702

You're using a mysqli DB connection, but calling mysql to do your select. You cannot mix/match the database libraries like that. If you'd had even minimal error checking, you'd have been told that there's no connection to the db:

$result = mysql_query($select) or die(mysql_error());
                               ^^^^^^^^^^^^^^^^^^^^^

Plus, your select query has syntax errors. 'daycounter' is a string literal - you cannot select FROM a string. 'datum' would be syntactically correct, you can select a string literal from a table, but most like you want:

SELECT datum FROM daycounter
or
SELECT `datum` FROM `daycounter`

Neither of those words are a reserved word, so there's NO need to quote them, but if you're one of those people who insist on quoting ALL identifiers, then they must be quoted with backticks, not single-quotes.

Upvotes: 2

Related Questions