karhu
karhu

Reputation: 119

Why isn't the "message" field being pulled from my MYSQL database?

I'm trying to return the "message" field from a random DB entry (in the messages table of my database) in the body of my homepage.

This is my index.php file. I've verified through connect.php that the link with the database is working.

<html>
<body>

<?php 
include_once('connect.php');
$quote=$_GET['message'];
$sql="SELECT message FROM messages ORDER BY RAND()LIMIT 1";
$results=mysql_query($sql, $link);
while(list($message)=mysql_fetch_array($results)){
echo $message;
}

mysql_close();


?>

</body>
</html>

Can you see what I'm doing wrong? I'm sure I've made an egregious error somewhere.

Thank you for your help!

Upvotes: 0

Views: 48

Answers (1)

Marc B
Marc B

Reputation: 360572

Typo:

$sql="SELECT message FROM messages ORDER BY RAND()LIMIT 1";
                                                  ^--missing a space

If you'd had even bare minimum query error handling, you'd have detected this:

$results=mysql_query($sql, $link) or die(mysql_error());

never EVER assume a query has succeeded. Even if the SQL syntax was 100% valid, there's far too many OTHER reasons for things to blow up to NOT check for failure.

Upvotes: 3

Related Questions