Help me with Warning: mysql_num_rows(): please

Discussion in 'PHP' started by ziya, Jul 15, 2007.

  1. #1
    Hey guys,

    Can any one help me with this error :

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in z:\home\localhost\www\test\my-site\top.php on line 70

    ----------
    in top.php :

    $get_language=mysql_query("select * from tbl_language where is_active='t'");
    if (mysql_num_rows($get_language)>1) {

    Why do i get the error on my machine ? it works fine on online , server ..

    Thank you all
     
    ziya, Jul 15, 2007 IP
  2. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #2
    you're passing a result set to mysql_num_rows, not a link identifier. if you only have one connection to the db, just use mysql_num_rows()

    
    if ( mysql_num_rows() > 1 ) { ... }
    
    PHP:
     
    ansi, Jul 15, 2007 IP
  3. nabil_kadimi

    nabil_kadimi Well-Known Member

    Messages:
    1,065
    Likes Received:
    69
    Best Answers:
    0
    Trophy Points:
    195
    #3
    Try this query directly on phpMyadmin or on the MySQL console of your machine
    select * from tbl_language where is_active='t';
     
    nabil_kadimi, Jul 15, 2007 IP
  4. ziya

    ziya Well-Known Member

    Messages:
    1,971
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #4
    Yes , it works fine on phpMyadmin . I got 3 rows from there .

    I tryed as you but i got the same error. I tyred this too :

    $get_language=mysql_query("select * from tbl_language where is_active='t'");
    $count2=mysql_num_rows();

    or like this :

    $get_language=mysql_query("select * from tbl_language where is_active='t'");
    $count2=mysql_num_rows($get_language);

    But no luck.. is it becouse of settings , or problem with connecting to database with top.php ?
     
    ziya, Jul 15, 2007 IP
  5. Chemo

    Chemo Peon

    Messages:
    146
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #5
    mysql_error()
     
    Chemo, Jul 15, 2007 IP
  6. Brewster

    Brewster Active Member

    Messages:
    489
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #6
    Have you tried displaying the rows returned by the query ? That may help to reveal where the problem is.

    Brew
     
    Brewster, Jul 15, 2007 IP
  7. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #7
    Are you sure? I decided to verify and yes, if you are going to supply an argument, it is supposed to be a result set, not a link identifier, see php.net docs:
    mysql_num_rows()
    PHP:
    ziya, get into a habit of catching errors. A simple thing to do in this case is to use this at the end of each query:
    mysql_query('SELECT * FROM table...') or die(mysql_error());
    PHP:
     
    krt, Jul 15, 2007 IP
  8. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #8
    whoops yeah sorry. was thinking of mysql_affected_rows()
     
    ansi, Jul 15, 2007 IP
  9. ziya

    ziya Well-Known Member

    Messages:
    1,971
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #9
    --------
    ziya, get into a habit of catching errors. A simple thing to do in this case is to use this at the end of each query:
    mysql_query('SELECT * FROM table...') or die(mysql_error());
    PHP:
    [/QUOTE]
    -----------

    Thanks for tellig the tip.. I did as u told And i got error
    "No database selected " error..

    How can i check this ?

    Thank you all
     
    ziya, Jul 16, 2007 IP
  10. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #10
    Use mysql_select_db('your_database_name') beforehand to select a database to query.
     
    krt, Jul 16, 2007 IP
  11. ziya

    ziya Well-Known Member

    Messages:
    1,971
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #11
    thanks all for your help.. I fixed that error.. I had there :

    db.php in wich there were writen config.php in require. And i got lost in that include and reuqire files.. i have deleted config.php. i copied and pasted all from config.php to db.php.. and i had full scene infront of me . how do you think what is the best way to use db.php or config.php or both on web projects ?

    but i have another error. i will try to fix that error by myself..
     
    ziya, Jul 16, 2007 IP
  12. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #12
    a file with the database information is always a good idea. then you dont have to worry about going through and editing out passwords and all if you show your code to someone else to help debug. aside from that, it makes switching database servers easily. it's 4 lines (hostname,username,password,database) in 1 file instead instead of 4 lines in 100 files. as for the config.php, it depends on if you're writing the script to be released. if so, then yes, it is a good idea to make it easier on the user. if not, there's really no need as you know what everything does.
     
    ansi, Jul 16, 2007 IP