What is the difference..........!

Discussion in 'PHP' started by strgraphics, Jun 21, 2010.

  1. #1
    Friends.., this may be the simple but... i need the exact answer.

    $res = mysql_query("SELECT * FROM $table1 WHERE email = '$eml'");
    
    if ($res)
    {
    
    content
    
    } 
    PHP:
    And Another is..,

    $res = mysql_query("SELECT * FROM $table1 WHERE email = '$eml'");
    
    if (mysql_num_rows($res)==1)
    {
    
    content
    
    } 
    PHP:
    What is the diffreence between them.., exactly...., exactly....!

    Thanks a lot.....!
     
    strgraphics, Jun 21, 2010 IP
  2. HuggyEssex

    HuggyEssex Member

    Messages:
    297
    Likes Received:
    4
    Best Answers:
    2
    Trophy Points:
    45
    #2
    The first one is just checking whether $res is set more then 0, it's not checking if the query has returned a true result. The second one is checking if that query has found a result equalling 1 for the query.
     
    HuggyEssex, Jun 21, 2010 IP
  3. mfscripts

    mfscripts Banned

    Messages:
    319
    Likes Received:
    4
    Best Answers:
    8
    Trophy Points:
    90
    Digital Goods:
    3
    #3
    Top one checks the result of the mysql_query() function, false or the resource if successful. The 2nd one checks that the amount of rows with the result is equal to 1.
     
    mfscripts, Jun 21, 2010 IP
  4. Michellu

    Michellu Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    mysql_query() returns a resource if the query was successfully run or false on error (if either the query was incorrect or the user didn't have permission etc). If the query was run successfully but there were no results in the database matching that query it still returns a resource.

    That being said, the first piece of code checks if the query was successfully run and the second checks if the query returned only one row (only one row in the database matched the query, the query was run successfully and the one row was returned successfully).
     
    Michellu, Jun 21, 2010 IP
  5. strgraphics

    strgraphics Active Member

    Messages:
    710
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #5
    First one will execute the content whether the query is success or failure...

    and second will execute only if there the match is found i mean if it equal to one.., am i correct.


    What if not == 1
    
    $res = mysql_query("SELECT * FROM $table1 WHERE email = '$eml'");
    
    if (mysql_num_rows($res))
    {
    
    content
    
    }
    
    PHP:
     
    strgraphics, Jun 21, 2010 IP
  6. bvraghav

    bvraghav Member

    Messages:
    123
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    33
    #6
    pretty precise explanation.

    i would like to add that both the checks should to be used
    $q = "SELECT * FROM `table` WHERE 1 LIMIT 0, 30;";
    if ($r = mysql_query( $q, $con )) {
        if ( !($rec = mysql_fetch_assoc( $r )) ) {
            echo "empty result";
        } else {
            do {
                //process each record here
            } while( $rec = mysql_fetch_assoc( $r ) )
        }
    } else {
        echo "error in query ( " . mysql_errno($r) . "): " . mysql_error($r) . "<br />";
    }
    Code (markup):
    In the above example, the intention of checking the resource returned by mysql_query for falacy, is to check whether the query itself was valid, whereas, the intention of checking for mysql_num_rows($r) > 0, is to check if the result is empty or not.

    And more often, I do not need to use the value returned by mysql_num_rows, so i skip using that function, and use the value mysql_fetch_assoc (or may be mysql_fetch_array) instead. If the result is empty, the first row itself will return a null record on fetching.

    There is one more suggestion: check out for mysqli
    http://www.php.net/manual/en/mysqli.summary.php
    There are a few advantages in security matters and sql interjection. If you are a beginner to php-mysql, start learning the new thing straight away... :)
     
    bvraghav, Jun 21, 2010 IP