query error . Please help ASAP.

Discussion in 'MySQL' started by JEET, Jul 10, 2006.

  1. #1
    Hello,
    I keep getting this error when I run the below query.

    Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource

    My code:

    mysql_connect($dbhost,$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");
    $query="SELECT * FROM $tablename WHERE loc='$ckey'";
    $result=mysql_query($query);
    $num=mysql_numrows($result);

    What is happenning?
    Thank you
     
    JEET, Jul 10, 2006 IP
  2. shamess

    shamess Well-Known Member

    Messages:
    1,127
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    185
    #2
    It's mysql_num_rows ^^;; You just missed an underscore.
     
    shamess, Jul 10, 2006 IP
  3. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #3
    True but that would have caused an unknown function error. Seems like that missing _ might not be the only issue.
     
    T0PS3O, Jul 10, 2006 IP
  4. shamess

    shamess Well-Known Member

    Messages:
    1,127
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    185
    #4
    In that case, you may need to change ...

    $query="SELECT * FROM $tablename WHERE loc='$ckey'";

    into

    $query="SELECT * FROM `$tablename` WHERE `loc`='$ckey'";
     
    shamess, Jul 10, 2006 IP
  5. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #5
    It would be this in my style:

    
    $query = "SELECT * FROM " . $tablename . " WHERE loc='" . $ckey . "'";
    $result = mysql_query($query);
    $num = mysql_num_rows($result); 
    
    PHP:
    All nicely spaced...
     
    T0PS3O, Jul 10, 2006 IP
  6. shamess

    shamess Well-Known Member

    Messages:
    1,127
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    185
    #6
    Shouldn't it be..

    
          $query = "SELECT * FROM `" . $tablename . "` WHERE loc='" . $ckey . "'";
          $result = mysql_query($query);
          $num = mysql_num_rows($result); 
    
    PHP:
    Since what you said is exactly the same as is already being done, just looking prettier? xP

    JEET, can you tell us what $tablename is?
     
    shamess, Jul 10, 2006 IP
  7. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #7
    Hi,
    I tried all your methods but I am still getting the error.
    The code is :

    <?
    mysql_connect($dbhost,$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");

    $query="SELECT * FROM $tablename";
    $result=mysql_query($query);
    $num=mysql_num_rows($result);

    echo $num;
    ?>

    See it at:
    http://www.ad4business.com/1/w/admin/showall.php

    It should at least echo 0 even if the db is empty...
    What to do?
     
    JEET, Jul 10, 2006 IP
  8. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #8
    I AM A BIG FAT IDIOT.
    The password I was using to connect to db was wrong.
    It's fine now.
    A million thanks to all you who looked in this.
     
    JEET, Jul 10, 2006 IP
  9. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I never bother with the comma's around the table name. So annoying because there's a `, ', " etc. So I got used to leaving them out entirely.

    JEET, it echoes the first $num, then it goes pearshape.... EDIT: see you got it...
     
    T0PS3O, Jul 10, 2006 IP
  10. shamess

    shamess Well-Known Member

    Messages:
    1,127
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    185
    #10
    Heh, easy mistake ^^;; I always do that and spend hours looking at the wrong line trying to fix it.
     
    shamess, Jul 10, 2006 IP
  11. vectorgraphx

    vectorgraphx Guest

    Messages:
    545
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #11
    "When you have eliminated all which is impossible, then whatever remains, however improbable, must be the truth."

    - Sherlock Holmes
     
    vectorgraphx, Jul 10, 2006 IP