0

I'm trying to retrieve the email field from my database using the id associated with it:

$query2 = mysql_query("SELECT email FROM `users` WHERE `id` = '".$userID."' ") or die(mysql_error());

This query is always returning NULL and I can't work out why.

Have tested using a var_dump that $userID is indeed correct.

But when I use it with the hardcoded value instead of $userID it works fine:

$query2 = mysql_query("SELECT email FROM `users` WHERE `id` = '85' ") or die(mysql_error());

Why isn't the $userID variable being passed to my query? Is there a way to pass this correctly?

Edit:

Declaration of $userID as requested. var_dump of this variable works OK the line before the query2.

// Fetch ID for matching details
    $query = mysql_query("SELECT id FROM `users` WHERE `email` = '".$emailInput."' && `username` = '".$usernameInput."' ") or die(mysql_error());


        // Successful query - ID stored
        if(mysql_num_rows($query) > 0){
        $userID = mysql_fetch_array($query);}

        var_dump($userID);

Both var_dumps output the following on the page:

array(2) { [0]=> string(2) "85" ["id"]=> string(2) "85" } NULL
Francesca
  • 26,842
  • 28
  • 90
  • 153

5 Answers5

3

Id say the the fact that $userID is an array is your problem... Do $userID['id'] instead in the query.

$query2 = mysql_query("SELECT email FROM `users` WHERE `id` = '" . $userID['id'] . "' ") or die(mysql_error());
superphonic
  • 7,954
  • 6
  • 30
  • 63
0

try removing either double quotes or single quotes. because id is usually of data type int you are using twice which makes it a string value..

also PHP is quite smart in recognizing variables and data types so u can use "$userID" or "{$userID}" without concatination..

Aous1000
  • 2,052
  • 3
  • 16
  • 16
0

Its probably a concatenation problem , if the $userid is string its should be fine , but if its an integer or double..etc it will be dealt with as a string

if its an integer try :

$query2 = mysql_query("SELECT email FROM `users` WHERE `id` = ".$userID) or die(mysql_error());

or

$query2 = mysql_query("SELECT email FROM `users` WHERE `id` = ".$userID['0']) or die(mysql_error());
xyzdev
  • 23
  • 7
0

so, the problem is that your $userID is not 85 or a simple number but it's an array and you are still trying to concatenate it in the query.

The problem is somewhere else in your code, probably where you set $userID

Here goes the solution:

$sql = mysql_query("SELECT email FROM `users` WHERE `id` = " . $userID['id']) or die(mysql_error());
ponciste
  • 2,231
  • 14
  • 16
0

Try this ....

$query2 = mysql_query("SELECT email FROM `users` WHERE `id` = $userID ") or die(mysql_error());
Shashidhar Gr
  • 420
  • 2
  • 6
  • 15