Jump to content


This topic is now archived and is closed to further replies.


mysql_num_rows error

Recommended Posts

okey, i wrote this guestbook script for an assignment, and i have this error

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in W:\www\guestbook.php on line 41

i can't find the error though :S, sorry new to php and mysql so any help would be great.
here is my code
the href is on one line too, even though it looks like 2
    $connection = @mysql_connect($db_host, $db_user, $db_password) or die("error in connection");
    mysql_select_db($db_name, $connection);

    $name = $_POST["txt_name"];
    $len = strlen($name);
    //Only write to database if there's a name
    if ($len > 0)
        $email = $_POST["txt_email"];
        $comment = $_POST["txt_comment"];
        $date = time();

        $query = "INSERT INTO guestbook (autoID, name, email, comment, date_auto) VALUES (NULL, '$name', '$email', '$comment', '$date')";
        mysql_query($query, $connection) or die(mysql_error());

<form action="<?php echo $_SERVER[PHP_SELF]; ?>" method="POST">
    <font face="arial" size="1">
        Name: <input type="text" name="txt_name"> 
        Email: <input type="text" name ="txt_email"><br><br>
        Comment: <br>
        <textarea style="width: 75%" Rows="10" name="txt_comment"></textarea>
        <CENTER><input type="submit" value="Submit"></CENTER>

<table bgcolor="#AAAAAA" border="0" width="75%" cellspacing="1" cellpadding="2">
    $query = "SELECT * FROM guestbook ORDER BY date_order";
    $result = mysql_query($query, $connection);

    for ($i=0; $i < mysql_num_rows($result); $i++)
        $name = mysql_result($result, $i, "name");
        $email = mysql_result($result, $i, "email");
        $email_len = strlen($email);
        $comment = mysql_result($result, $i, "comment");
        $comment = nl2br($comment);
        $date = mysql_result($result, $i, "date_auto");
        $show_date = date("H:i:s m/d/Y", $date);

        if ($i % 2)
        echo '
                <td width="100%" bgcolor="'.bg_color.'">
                    <font face="arial" size="2">';
                        if ($email_len > 0)
                            echo '<b>Name:</b> <a href="mailto:'.$email.'">'.$name.'</a>';
                            echo '<b>Name:</b> '.$name;
                        echo '
                        <b>Comment:</b> '.$comment.'
                <td width="1%" valign="top" nowrap bgcolor="'.bg_color.'">
                    <font face="arial" size="2">
                        <b>Date: </b> '.$show_date.'



Share this post

Link to post
Share on other sites
hi mate try this

$result = mysql_query($query, $connection) or die(mysql_error());

it will tell you if you have an error in your SQL statement as this will be what is causing your problem..

I'm not 100% but dont you need to tell it how to order? DESC or ASC

try this

$query = "SELECT * FROM guestbook ORDER BY date_order DESC";

should resolve your issue, have left the or die(mysql_error()); as this is very handy to see what is wrong with the query :smiley:


Share this post

Link to post
Share on other sites
thanks :) works now. i had a field written wrong. was date_order as oppose to date_auto in the $query variable :$

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.