0

So I am trying to see if a set of numbers is in an array.

array(3) { 
     [0]=> array(2) { 
        ["account_ID"]=> string(10) "1781890863" 
        [0]=> string(10) "1781890863" 
      } 
     [1]=> array(2) { 
        ["account_ID"]=> string(10) "2093832999" 
        [0]=> string(10) "2093832999" 
      } 
     [2]=> array(2) { 
        ["account_ID"]=> string(10) "2147483647" 
        [0]=> string(10) "2147483647" } 
      } 

I have the array and the values are all there and everything is just dandy. But when I compare using in_array it returns false. I don't quite know why.

class DB(){

    public function getAllID(){
        $result_row = $this->accessDB( 'SELECT account_ID FROM users;');
        return $result_row; 
    }
}

That is the function that I am using to access the database and return the array and then

$app = new DB();
if(isset($_GET['user'])){
    if(in_array($_GET['user'],$app->getAllID())){
        include('account.php');
        echo 'Account DOES exist';
    } else {
        var_dump($app->getAllID());
        echo '<br/>'.$_GET['user'].' does NOT exist.';
    } 
}

Does anyone see why my code here won't work maybe I am just accessing the DB wrong?

5
  • what does the var_dump($app->getAllID()); look like? Commented Mar 9, 2014 at 19:43
  • 1
    return $result_row; will return an associative array so yes the way you are trying will not work Commented Mar 9, 2014 at 19:43
  • The var_dum($app->getAllID()); Is above. Commented Mar 9, 2014 at 19:44
  • is the value of $_GET['user'] 0, 1, or 2? Commented Mar 9, 2014 at 19:44
  • The value of $_GET['user'] should be one of the items of the array. When It is set to one of the items it returns false, when it it set to a number other than that in the array it returns false. Commented Mar 9, 2014 at 19:46

3 Answers 3

2

Your method returns array of assoc array, instead of array of int/string (on which your check is working).

Easiest solution - pull account_ID one level up.

public function getAllID(){
    $result_row = $this->accessDB( 'SELECT account_ID FROM users;');
    return array_map(function($entry) { return $entry['account_ID']; }, $result_row); 
}

Better solution - validate with DB query:

// must escape $userId before doing query or you are vulnerable to SQL injection

$this->accessDB("SELECT count(account_ID) FROM users WHERE account_ID = {$userId} LIMIT 1");
Sign up to request clarification or add additional context in comments.

3 Comments

This is a good solution. Using array_map is much cleaner than the answer I posted. I also like that you mentioned validating the specific user rather than querying for all user ids
If I were to validate with a DB query would I still return the array_map or would I return $result_row ?
As query returns only one field in one row (the count) I would convert that one value to bool. Using var format you gave in question your count value, return (1 == $result_row[0][0]); should work for isUserFound($userId)
0

You need to loop through $app->getAllID() because if the $_GET['user'] is there it will be in $app->getAllID()[0], $app->getAllID()[1] etc..

Really, you should do this check for $_GET['user'] in the query with a WHERE clause and return true if it exists and false if not. Why query and return all account_IDs just to check if one exists?

Comments

0

Your function pulling the query results is returning a multi-deminsional array. You can either loop through the array to determine if $_GET['user'] is in each array within or change your function that is pulling the user ids.

In your function loop through your query results and build your array:

$user_ids = array();
foreach ($result_row as $record) {
    $user_ids[] = $record['account_ID'];
}
return $user_ids;

This will create an array of user ids. Then you can use in_array() to determine if the user id exists.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.