I'm trying to run this code but there is error message:

PHP Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource

<?php

if (mysql_connect('localhost','root','') && mysql_select_db('guestbook'))
    {
        //echo 'connected today';
        $time= time();
        $errors= array();
    if (isset($_POST['guestbook_name'], $_POST['guestbook_email'], $_POST['guestbook_message']))
        {
             $guestbook_name = mysql_real_escape_string(htmlentities($_POST['guestbook_name']));
             $guestbook_email = mysql_real_escape_string(htmlentities($_POST['guestbook_email']));
             $guestbook_message = mysql_real_escape_string(htmlentities($_POST['guestbook_message']));

            if  (empty($guestbook_name) || empty($guestbook_email) ||  empty($guestbook_message))
                {
                     $errors[] = 'All the feilds are required';
                }

                if (strlen($guestbook_name)>25 || strlen($guestbook_email)>255 || strlen('$guestbook_message')>255)
                     {
                          $errors[] = 'One or more feilds exceeded the character limit';
                     }
                        if (empty($errors))
                             {
                                $insert ="INSERT INTO 'entries' VALUES ('','$time','$guestbook_name','$guestbook_email','$guestbook_message')";
                                if (mysql_query($insert))
                                     {
                                         //echo 'sucess';
                                         header('Location: '.$_SERVER['PHP_SELF']);

                                     }
                                             else
                                                 {

                                                    $errors[]= 'Something went wrong, try again';
                                                 }

                            }
                                             else
                            {
                                foreach($errors as $errors)
                                     {
                                         echo '<p><strong>'.$errors. '</strong></P>';
                                     }
                            }
        }
    //display data


    $entries = mysql_query("SELECT  'timestamp','name','email','message' FROM 'entries' ORDER BY 'timestamp' DESC");
    if (mysql_num_rows($entries)==0)
        {
        echo 'No entries,yet';
        }
             else
                {
                     while($entries_row= mysql_fetch_assoc($entries))
                        {

                            $entries_timestamp =date('D M Y @ h:i:s',$entries_row['timestamp']);
                            $entries_name =$entries_row['name'];
                            $entries_email =$entries_row['email'];
                            $entries_message =$entries_row['message'];
                            echo '<p><strong>Posted by '.$entries_name.' ('.$entries_email.') on '.$entries_timestamp.'</strong>:<br> '.$entries_message.'</P>';
                         }
                           // echo'Entries found';
                }
    }
        else
                {
        echo 'could not connect! oh no problem again';
                }
?>
<hr>
<hr>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="POST">
            <strong>Post something...</strong><br>
            Name:<br><input type="text" name="guestbook_name" maxlength="25"><br>
            Email:<br><input type="text" name="guestbook_email" maxlength="255"><br>
            Message:<br><textarea name="guestbook_message" rows="5" cols="40 "maxlength="255"></textarea><br>
            <input type="submit" value="post">
</form>
<hr>
<hr>

You can view the error message by typing the following:

$entries = mysql_query("SELECT  timestamp,name,email,message FROM entries ORDER BY timestamp DESC") or die(mysql_error());

This will show you the reason why you are facing such an error message. It might be because you have mistyped the name of some field or the name of the table itself.

There should be error in your query. Just echo your query as follows

echo "SELECT  'timestamp','name','email','message' FROM 'entries' ORDER BY 'timestamp' DESC";
  $entries = mysql_query("SELECT  'timestamp','name','email','message' FROM 'entries' ORDER BY 'timestamp' DESC");

and in output page copy that query and paste it in the sql section of your phpmyadmin and see what is the result.

I would go with saying it's likely an error on your behalf, don't worry we all make them from time to time! Open up your database table, check all the names, names ARE case sensitive so also make sure you have the right casing for the name of the column.

But still, as said above by others, echo out your query, and take a look at the error, it will most likely point you to a bit of your query string.

Kind Regards,
Jack.

I put the query in the myphp admin sql section: this was the message:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '$entries = mysql_query(&quot;SELECT 'timestamp','name','email','message' FROM 'entri' at line 1

Just put the following query only in the SQL section of phpMyAdmin.

SELECT  'timestamp','name','email','message' FROM 'entries' ORDER BY 'timestamp' DESC

As kartik said you only need to run query part in phpmyadmin, not PHP.

Replace the single quotes(') with (`) as follows. It should work.

SELECT  `timestamp`,`name`,`email`,`message` FROM `entries` ORDER BY `timestamp` DESC

Guys! The error that is shown in phpMyAdmin is the same that appears when you type the OR DIE statement as I typed above.

$entries = mysql_query("SELECT timestamp,name,email,message FROM entries ORDER BY timestamp DESC") or die(mysql_error());

This will, for sure, show you the problem you're encountering.

Thanks a lot guys. SOLVED.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.