Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 8 of 8
  1. #1
    Senior Member Brak's Avatar
    Join Date
    Apr 2003
    Location
    San Francisco, CA
    Posts
    3,413
    Member #
    1217
    Liked
    2 times
    Just wanted the fastest way of checking if a particular sql statement returns any rows. I tried the search function but got mad at the ($*&%$ 3 word minumum, so sorry if this has been asked a bunch
    Kyle Neath: Rockstar extraordinare
    The blog | The poetry site | The Spore site

  2.  

  3. #2
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    Theoretically, this is fast:
    Code:
    SELECT IF(COUNT(column) > 0, 1, 0) FROM table WHERE condition
    You can also do:
    Code:
    SELECT ... LIMIT 1
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!

  4. #3
    Senior Member Brak's Avatar
    Join Date
    Apr 2003
    Location
    San Francisco, CA
    Posts
    3,413
    Member #
    1217
    Liked
    2 times
    Well... I mean more talking php methods, should have cleared that up. Basically checking to see if [minicode] $sql = "SELECT username FROM users WHERE username = '$username'";
    [/minicode] returns a row or an empty set .
    Kyle Neath: Rockstar extraordinare
    The blog | The poetry site | The Spore site

  5. #4
    JR
    JR is offline
    Senior Member JR's Avatar
    Join Date
    Nov 2002
    Location
    UK
    Posts
    4,354
    Member #
    257
    could you try to echo it using php? It's what i always do to check if it works (newb trying to sound clever here )
    JR

  6. #5
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    Try calling mysql_fetch_array twice. If the second time returns false, then it only returned one row.
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!

  7. #6
    Senior Member Brak's Avatar
    Join Date
    Apr 2003
    Location
    San Francisco, CA
    Posts
    3,413
    Member #
    1217
    Liked
    2 times
    Well I was wondering if that's the fastest way.

    for example, I KNOW I can use this:
    PHP Code:
    $sql "SELECT username FROM users WHERE username = '$username'";
    $result mysql_query($sql);
    $temp mysql_fetch_array($result);
    if (
    $temp["username"]) echo "Let the world bring frogs upon us all!"
    But I was wondering the fastest way of doing so
    Kyle Neath: Rockstar extraordinare
    The blog | The poetry site | The Spore site

  8. #7
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    PHP Code:
    $result mysql_query(...);
    echo 
    mysql_num_rows($result) . " rows returned"
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!

  9. #8
    Member nepharo's Avatar
    Join Date
    Jun 2003
    Posts
    73
    Member #
    1729
    I would do:

    PHP Code:

    $result 
    mysql_query(...);

    if(
    mysql_num_rows($result) > 0)
    echo 
    "has at least 1"

    edit: Sorry, I just saw what filb wrote. i guess I mistakenly skipped over it. It's basically the same.
    I cannot self terminate, you must lower me into the fire.


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 12:35 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com