1

there is an email address in the user session, and I am trying to use the extracted info to access the other relevant info on the user from the database. Here is what I have.

$author = explode(":", $_SESSION['user_id']);

$query = mysql_query("SELECT * FROM users WHERE `active`='1' AND `email`=$author[1]");

while ($query_row = mysql_fetch_assoc($query)) {

// does stuff in here.

}

for some reason I get back "mysql_fetch_assoc() expects parameter 1 to be resource, boolean given." If I echo out "$author['1']" I get the correct email address for the logged in user. How do I go about writing the query correctly?

Guage
  • 85
  • 10

4 Answers4

1

You should put:

"SELECT * FROM users WHERE active='1' AND email='".$author[1]."'"

make sure you have propper quotes up there

0
$qry ="SELECT * FROM users WHERE `active`='1' AND `email`= ".$author['1'];
$query = mysql_query($qry);
Deepak Rai
  • 2,163
  • 3
  • 21
  • 36
0
$query = mysql_query("SELECT * FROM users WHERE `active`='1' AND `email`='".$author[1]."'");
M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118
Adnan
  • 579
  • 1
  • 4
  • 11
0

You can enclose variable in quotes with curly brackets:

$query = mysql_query("SELECT * FROM users WHERE `active`='1' AND `email`={$author[1]}");

Although I have one more tip: run queries like this : $query = mysql_query('...') or die(mysql_error()); With this you will at least know why it fails.

P.S. I guess you have a reason to use mysql extension, but anyway you should use PDO or mysqli over mysql. You get placeholders with mysqli, or named parameters with PDO. Much nicer to work with.

FDIM
  • 1,981
  • 19
  • 21