Jump to content

Recommended Posts

i followed a video tutorial to make a guestbook using php but when i fininshed typing the file and executed it. it showed me an error that says "Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\wamp\www\myguestbook.php on line 38" and line 38 has the following "  for ($i = 0; $i < mysql_num_rows($result); $i++)"


and here is the script :

[code]
<?php
require ($_SERVER["DOCUMENT_ROOT"]."/config/db_connect.php");
$connection = @mysql_connect ($db_host, $db_user, $db_password) or die ("error connecting");
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>
<body>
<center>
<form action="<?php echo $_SERVER[PHP_SELF]; ?>" method="post">
<font face="arial" size="1">
     Name: <input type="text" name="txt_name">&nbsp;
     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"</font></center>


</form>
<table bgcolor="#AAAAAA" border="0" width="75%" cellspacing="1" cellpading="2"
<?php
$query="SELECT * FROM questbook ORDER BY date_auto";
$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= strlen($email);
     $comment = mysql_result($result,$i, "comment");
     $date = mysql_result($result,$i, "date_auto");
     $show_date ("H:i:s m/d/Y" , $date);
     echo '
     <tr>
     <td width="50%" bgcolor="#EEEEEE">
     <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="#EEEEEE">
     <b>Date: </b> '.$show_date.'
     </font>
     </td>
     </tr>
      ';

     }
     ?>
</table>
</center>
</body>
</html>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/34118-using-mysql_num_rows-within-for/
Share on other sites

change this line, just before your error: $result = mysql_query($query, $connection); to this

[code]
$result = mysql_query($query, $connection) or die(mysql_error());
[/code]

the error is a result of a problem getting a resource id as a result (possibly) of the query failing.

[b]edit[/b] also, when first writing code and hence needing to debug/test as you go, try to avoid the @ error supressor that you used in your first mysql_connect. if the query i mentioned above is valid, then chances are there has been an error connecting properly to your database.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.