Error message that includes: mysql_num_rows():

Discussion in 'MySQL' started by pentester101, Apr 20, 2009.

  1. #1
    Hello,

    I have received the following error message on my website:

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/ptremail/public_html/admin/newmail.php on line 431

    listed below is an excerpt from that file it is lines 429 to line 439:

     <?
    	$sql=mysql_query("SELECT * FROM reads ORDER BY fnum");
    	$rows=mysql_num_rows($sql);
    	for($i=0;$i<$rows;$i++)
    	{
    	mysql_data_seek($sql,$i);
    	$arr=mysql_fetch_array($sql);
    	extract($arr);
    	$sq=mysql_query("SELECT * FROM signtask WHERE tasknum=$fnum");
    	$signups=mysql_num_rows($sq);
    	echo"
    PHP:

    Any help would be most appreciated. Thanks!
     
    pentester101, Apr 20, 2009 IP
  2. lp1051

    lp1051 Well-Known Member

    Messages:
    163
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #2
    Hi, the error tells you your SQL query is somehow wrong - "SELECT * FROM reads ORDER BY fnum" - so first of all do you have table 'reads' and column 'fnum'??
     
    lp1051, Apr 20, 2009 IP
  3. pentester101

    pentester101 Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yes, I checked for that and I do have a table 'reads' and a column 'fnum'. I also checked the database connectivity and it is working.
     
    pentester101, Apr 20, 2009 IP
  4. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #4
    Use mysql_error() to know the exact error.
     
    mwasif, Apr 21, 2009 IP
  5. pentester101

    pentester101 Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    OK, When I did that I got the following:

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/ptremail/public_html/admin/newmail.php on line 431

    Warning: mysql_data_seek(): supplied argument is not a valid MySQL result resource in /home/ptremail/public_html/admin/newmail.php on line 435

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/ptremail/public_html/admin/newmail.php on line 436

    Warning: extract() [function.extract]: First argument should be an array in /home/ptremail/public_html/admin/newmail.php on line 437

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/ptremail/public_html/admin/newmail.php on line 439
     
    pentester101, Apr 21, 2009 IP
  6. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #6
    You didn't get what I want to say. Use mysql_error() in this manner

    $sql=mysql_query("SELECT * FROM reads ORDER BY fnum") or die(mysql_error());
    Code (markup):
     
    mwasif, Apr 21, 2009 IP
  7. kjambu

    kjambu Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    i don't have mysql or php environment immediately with me.
    but as the ocde looks so simple, i would like to look at two things.

    1. Is there data in the table?

    2. remove the order by clause and try.
     
    kjambu, Apr 21, 2009 IP
  8. pentester101

    pentester101 Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8

    Ok, when I did that I got the following:

    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 'reads ORDER BY fnum' at line 1
     
    pentester101, Apr 21, 2009 IP
  9. pentester101

    pentester101 Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9

    1. Is there data in the table?

    Yes, there is data in the table

    2. remove the order by clause and try.

    Did this and I still get the same error.
     
    pentester101, Apr 21, 2009 IP
  10. kjambu

    kjambu Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    ?php
    $con = mysql_connect("localhost", "peter", "abc123");
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }$db_selected = mysql_select_db("test_db",$con);$sql = "SELECT * FROM reads";
    $result = mysql_query($sql,$con);
    echo mysql_num_rows($result);mysql_close($con);
    ?>
     
    kjambu, Apr 21, 2009 IP
  11. kjambu

    kjambu Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    i may be wrong, but i see mysql_query call needs 2 parameters.
    in your code the second parameter is missing
     
    kjambu, Apr 21, 2009 IP
  12. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #12
    reads is a reserved word. You need to use backticks around reads e.g.
    $sql=mysql_query("SELECT * FROM `reads` ORDER BY fnum") or die(mysql_error());
    Code (markup):
     
    mwasif, Apr 21, 2009 IP
  13. pentester101

    pentester101 Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    That was it! Thanks so much! Reps awarded!
     
    pentester101, Apr 21, 2009 IP
  14. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #14
    You are Welcome!
     
    mwasif, Apr 21, 2009 IP