Yet Another mysql_num_rows() Error

Discussion in 'PHP' started by The_Phantom, Jan 18, 2015.

  1. #1
    Hi Guys,

    I've been beating myself up trying to correct this to no avail. I'm hoping that one of you phpGurus can assist. I need to get the following working:
    <?php
      $sql = "select * from users where userid = " . $user->userid;
    $query = mysql_query($sql);
    
    if (mysql_num_rows($query)) {
       $result = mysql_fetch_array($query);
         $game = $result['game'];
    }
    
    if (!empty($_POST['game'])) $game = $_POST['game'];
    ?>
    Code (markup):
    Thanks!
     
    The_Phantom, Jan 18, 2015 IP
  2. 2WDH.com

    2WDH.com Active Member

    Messages:
    143
    Likes Received:
    3
    Best Answers:
    5
    Trophy Points:
    68
    #2
    Hi.

    What error do you see when trying to run the mentioned code?
     
    2WDH.com, Jan 18, 2015 IP
  3. The_Phantom

    The_Phantom Well-Known Member

    Messages:
    470
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    110
    #3
    Hi,

    The error given is:

    Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /home/xxxx/public_html/header.php on line 11

    Actually, I'm having quite a few mysql_num_rows() & mysql_fetch_array() errors lately. I thought if someone could help me with this error, I can probably use the same info to take care of the others.

    Thanks!
     
    Last edited: Jan 18, 2015
    The_Phantom, Jan 18, 2015 IP
  4. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #4
    When you get such error, it means that the query is invalid. You can try outputting the query and you will see what is wrong with it (for example if the user id is empty).
     
    ThePHPMaster, Jan 18, 2015 IP
  5. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #5
    You should also stop using mysql_ - it's been outdated for 8+ years. Use mysqli_ or PDO instead. Btw, why are you using mysql_num_rows to check if it returns a result? Just do the if directly on mysql_fetch_array
     
    PoPSiCLe, Jan 18, 2015 IP
    deathshadow and digitalpoint like this.
  6. 2WDH.com

    2WDH.com Active Member

    Messages:
    143
    Likes Received:
    3
    Best Answers:
    5
    Trophy Points:
    68
    #6
    The_Phantom,

    1. It looks like your mysql_query returns false because of query error.
    You can use this to debug:
    $query = mysql_query($sql) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error());
    PHP:
    2. Check the "Recommended API" section at http://php.net/mysqlinfo.api.choosing
     
    2WDH.com, Jan 18, 2015 IP
  7. freelanceDeveloper

    freelanceDeveloper Well-Known Member

    Messages:
    59
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    118
    #7
    something like this should work but a lot can be improved on this :)

    
    // limit 1 to stop asa it finds a result assuming you don't use Auto increment as userid
    $query = "select * from users where userid = '" . $user->userid . "'   limit 1";
    $result = mysql_query($query);
    
    if ($result && mysql_num_rows($result))
    {
    $row = mysql_fetch_assoc($result);
    }
    
    Code (markup):
     
    Last edited: Jan 19, 2015
    freelanceDeveloper, Jan 19, 2015 IP
  8. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #8
    Why are you guys concocting SQL-queries? WHY?
    $query = "SELECT * FROM users WHERE userid = $user->userid LIMIT 1";
    This assumes that userid actually is a userid, numerical, no spaces etc. You don't have to enclose or escape numeric values in a query. In case it should be needed to do anything else, because the userid is an alphanumeric value, for instance:
    $query = "SELECT * FROM users WHERE userid = '$user->userid' LIMIT 1";
    No need to concoct anything in an sql-query
     
    PoPSiCLe, Jan 19, 2015 IP
  9. freelanceDeveloper

    freelanceDeveloper Well-Known Member

    Messages:
    59
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    118
    #9
    $query = "select * from users where userid = '" . $user->userid . "'   limit 1";
    Code (markup):
    $query = "SELECT * FROM users WHERE userid = '$user->userid' LIMIT 1";
    Code (markup):
    ( notice the single quotes )

    To me both seem identical, why I use '" . $user->userid . "' rather than '$user->userid' is simply my preferred coding style
    (& it better highlights in editors.)

    In the begin post indeed only numeric values are allowed, but maybe he uses the auto increment as userid ... who knows... even then it is advised to use quotes... but this is irrelevant to the question ;)

    btw : Do you mean concat ? :-D
     
    Last edited: Jan 20, 2015
    freelanceDeveloper, Jan 20, 2015 IP
  10. The_Phantom

    The_Phantom Well-Known Member

    Messages:
    470
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    110
    #10
    Got It ! Thanks For Your Help Guys!
     
    The_Phantom, Jan 21, 2015 IP
  11. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #11
    This is the best advice in the thread, and largely going unnoticed.

    There really is SO much wrong with that code -- the outdated mysql_ functions, the use of fetching an entire result set for no reason, using empty instead of (or in addition to) isset... blindly posting variable data into arrays like it's still 2005... You have NO business doing that if you care the slightest bit about security or efficiency. 2005 called, wants it's code back.

    Also does it make any sense to immediately change $game to the $_POST value right after setting it to the database's value? Should that perhaps be an ELSE?

    // assuming $db is a connected PDO object
    
    $statement = $db->prepare('
    	SELECT *
    	FROM users
    	WHERE userid = ?
    ');
    
    $stmt->execute([$user->userid]);
    
    if (!($game = $stmt->fetch())) {
    	$game = isset($_POST['game']) ? $_POST['game'] : false;
    }
    Code (markup):
    Guessing a bit, but I suspect this is what you were TRYING to do and how modern code would approach it.
     
    deathshadow, Jan 21, 2015 IP