Jump to content

Archived

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

wing_zero

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
[code]
<?php
    require($_SERVER["DOCUMENT_ROOT"]."/config/db_config.php");
    $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());
    }
?>

<html>
<head>
    <title>Guestbook</title>
</head>
<body>
<center>
<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>
    </font>
</form>

<table bgcolor="#AAAAAA" border="0" width="75%" cellspacing="1" cellpadding="2">
<?php
    $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)
        {
            $bg_bolor="#EEEEEE";
        }
        else
        {
            $bg_bolor="#E0E0E0";
        }
        
        echo '
            <tr>
                <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>';
                        }
                        else
                        {
                            echo '<b>Name:</b> '.$name;
                        }
                        echo '
                        <br>
                        <b>Comment:</b> '.$comment.'
                    </font>
                <td>
                <td width="1%" valign="top" nowrap bgcolor="'.bg_color.'">
                    <font face="arial" size="2">
                        <b>Date: </b> '.$show_date.'
                    </font>
                </td>
            </tr>
        ';
    }
?>

</table>

</body>
</center>
</html>
[/code]

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:

Regards
Liam

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.