Results 1 to 7 of 7

Thread: count rows returned in mysql

  1. #1
    Join Date
    Aug 2005
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default count rows returned in mysql

    how do i count how many rows in a table contain a certain string? what i have so far is:
    $result=mysql_query('SELECT username FROM users WHERE username=$username'); //($username = $_POST['username'])

    then i use mysql_num_rows($result) to see how many it returns, but php says:

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home2/haztuweb/haztuweb-www/gus/devvo/register_after.php on line 22

    i know that i am connected, i have selected the database properly, users is a table and username is a column...

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Because the query isn't returning anything. As you're using single quotes, the string doesn't get parsed before use; what you're sending literally contains the string '$username', rather than substituting in the contents of $username as you probably intend. Also, in order to avoid SQL injection attacks, you should addslashes() any user-provided data you use in a query.
    PHP Code:
    $result mysql_query('SELECT username FROM users WHERE username=' addslashes($username)); 
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Aug 2005
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    oh sorry, that was a mistake in my post. in the actual code, i was using double qoutes which if im right allow variables inside.. ill try adding addslashes to it
    thanks, gus

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Also, you need to supply a database connection to which you want to send the query as the second argument.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Aug 2005
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    the second argument is $link, the database connection and it still doesnt work... is it possible that it is showing me a warning because there are 0 rows returned?

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I don't think so, no; in any case, it certainly wouldn't give that warning. Are you sure the query is being executed OK?
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Aug 2005
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    ive sorted it out now, thanks for the help. i put 'or die(mysql_error())' on the $result line and mysql said something about the users table, so i dropped it and created it again and everything worked fine.

Bookmarks

Posting Permissions

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