SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: Quick Quetion

  1. #1
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Quick Quetion

    Currently I have the following code:
    PHP Code:
    $Check mysql_query("SELECT * FROM user_friends WHERE id='$ID' AND friend_id='$FriendID'");
    $CheckNumRows mysql_num_rows($Check);
    $CheckReverse mysql_query("SELECT * FROM user_friends WHERE id='$FriendID' AND friend_id='$ID'");
    $CheckReverseNumRows mysql_num_rows($CheckReverse);
    if (
    $CheckNumRows == && $CheckReverseNumRows == 0) {
        
    // do something


    I would like to know if the following code is basically the same thing as above but using only 1 sql statement:
    PHP Code:
    $Check mysql_query("SELECT * FROM user_friends WHERE (id='$ID' OR id='$FriendID') AND (friend_id='$ID' OR friend_id = '$FriendID')");
    $CheckNumRows mysql_num_rows($Check);
    if (
    $CheckNumRows == 0) {
        
    // do something

    Kayzio - We don't hesitate, we accelerate.

  2. #2
    SitePoint Enthusiast mithra62's Avatar
    Join Date
    Nov 2004
    Location
    Van Nuys
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    My head hurts

    Wow. My head hurts trying to figure this out.

    Technically, it looks like it should be comparable. Realistically, it's probably not a good idea to use the second approach though. Since:

    PHP Code:
    "SELECT * FROM user_friends WHERE (id='$ID' OR id='$FriendID') AND (friend_id='$ID' OR friend_id = '$FriendID')" 
    uses an OR type you run the risk of false positives. If you're worried about system resources with two queries you could work it out like

    PHP Code:
    "SELECT count(COLUMN) FROM TABLE" 
    Hope that helps.

  3. #3
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kayzio View Post
    Currently I have the following code:
    PHP Code:
    $Check mysql_query("SELECT * FROM user_friends WHERE id='$ID' AND friend_id='$FriendID'");
    $CheckNumRows mysql_num_rows($Check);
    $CheckReverse mysql_query("SELECT * FROM user_friends WHERE id='$FriendID' AND friend_id='$ID'");
    $CheckReverseNumRows mysql_num_rows($CheckReverse);
    if (
    $CheckNumRows == && $CheckReverseNumRows == 0) {
        
    // do something


    I would like to know if the following code is basically the same thing as above but using only 1 sql statement:
    PHP Code:
    $Check mysql_query("SELECT * FROM user_friends WHERE (id='$ID' OR id='$FriendID') AND (friend_id='$ID' OR friend_id = '$FriendID')");
    $CheckNumRows mysql_num_rows($Check);
    if (
    $CheckNumRows == 0) {
        
    // do something


    1) Your syntax is wrong, you can't take a string of a query and pass it to mysql_num_rows, you need to call mysql_query first.

    2) Otherwise your second query - while bit confusing is correct.

    I would think it makes more sense - following with the first query to order it like this..

    PHP Code:
    id '$id' AND friendID '$friendID' ) OR ( id '$friendID' AND friendID '$id' 
    This makes more sense to me anyway.

  4. #4
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help. I don't know why I didn't think of doing it the way you showed me wonshikee.
    Kayzio - We don't hesitate, we accelerate.


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
  •