Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jan 2010
    Posts
    45
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Angry mysql_num_rows(): supplied argument is not a valid

    Ok so I'm not completely new to php. Have done some basic scripts. Thought I would try and be clever by displaying information from one of my tables in mysql. That didn't go quite to plan.

    When I ran the script I got this error:"mysql_num_rows(): supplied argument is not a valid"

    Apparently the most common mistake is connecting to the wrong table, I have checked and re-checked this and I sure that I'm (also checked upper/lowercase ). I also ran a simple script to just connect to the database, that works.

    Any help would be great, thanks.

    This is my code:


    <?php

    //set variables to connect
    $host='localhost';
    $user='grahame2';
    $pass='Q2JKWP@F';
    $db='grahame2_test';


    //connect to mysql
    mysql_connect($host,$user,$pass,$db) or die("Unable to connect to the database");

    $query="SELECT * FROM family";

    $result=mysql_query($query) or die("Ivalid query");


    $num=mysql_num_rows($result);



    $i=0;
    while($i<num){

    $forename=mysql_result($result,$i,"forename");
    $surename=mysql_result($result,$i,"surname");
    $age=mysql_result($result,$i,"age");


    echo "$forename,$surname,$age";

    $i++;

    }

    ?>

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Actually the most common mistake is one you've made, and that is you are not checking the query call to see if it worked. Report any errors you find and you'll likely find the problem quickly thereafter.

    PHP Code:
    $result=mysql_query($query);

    //check the result
    if (!$result) {
        die(
    "Query error! text is $query<br>error is ".mysql_error());
    }
    $num=mysql_num_rows($result); 
    In case you're interested in the cause of the error you're seeing, it's simply telling you the mysql resource you passed to mysql_num_rows() isn't actually a mysql resource. Your $result variable will be a mysql resource if your call to mysql_query() works. If it fails, it will not be a valid mysql resource (it will instead be a boolean set to FALSE) and if you then pass it to a function that is expecting a mysql resource, you'll get the error you got.
    Last edited by Fumigator; 06-21-2010 at 08:17 PM.

  • #3
    New Coder
    Join Date
    Jan 2010
    Posts
    45
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Post Working(kind of)!

    Hi,

    Thanks for your post, really helped. Ok so the mistake I was making was not connecting to the database- which I resolved quite quickly (I think).

    But now I just see a blank page when I run the script. Here is the code revised:


    <?php

    //set variables to connect
    $host='localhost';
    $user='grahame2';
    $pass='Q2JKWP@F';
    $db='grahame2_test';


    //connect to mysql
    mysql_connect($host,$user,$pass);

    @mysql_select_db($db) or die( "Unable to select database");

    $query="SELECT * FROM family";

    $result=mysql_query($query);

    if (!$result){

    die("Query error! text $query<br> error is".mysql_error());


    }


    $num=mysql_num_rows($result);

    mysql_close();


    $i=0;
    while($i<num){

    $forename=mysql_result($result,$i,"forename");
    $surename=mysql_result($result,$i,"surname");
    $age=mysql_result($result,$i,"age");


    echo "$forename,$surname,$age";

    $i++;

    }






    ?>

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    PHP Code:
    while($i<num){ 
    You mean $num there, right? PHP is interpreting "num" as an undefined constant and your error reporting must be turned down low enough to not report it.

  • Users who have thanked Fumigator for this post:

    whatg (06-21-2010)

  • #5
    New Coder
    Join Date
    Jan 2010
    Posts
    45
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Cool :)

    Thanks, can't believe I didn't see that! Think I was expecting it to be something fundamentally wrong :-D


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •