Jump to content

MySQL Query with a PHP variable


sanchez77

Recommended Posts

Can anyone point out how to write a MySQL query with a PHP variable in the WHERE clause. I've tried {} {'xx'} and () and it still doesn't work.

 

Here is the code

 


<?php
ini_set('display_errors',1);
error_reporting(E_ALL|E_STRICT);

include ("include/connect.php");
include ("include/session.php");

$username = $session->userinfo['username'];

$result = mysql_query("SELECT email FROM customer WHERE user = {'$username'} ");

while($row = mysql_fetch_array($result))
  {
$custemail = $row['email'];
  }

echo "Session username: " . $username . "";
echo "Session customer email: " . $custemail . "";
?>

 

So I'm trying to show the email address for a record that matches the username of the user logged in.

 

I really appreciate the help.

 

 

Link to comment
https://forums.phpfreaks.com/topic/219029-mysql-query-with-a-php-variable/
Share on other sites

Here is the code I am using now:

 

<?php
ini_set('display_errors',1);
error_reporting(E_ALL|E_STRICT);

include ("include/connect.php");
include ("include/session.php");

$username = $session->userinfo['username'];

$result = mysql_query("SELECT email FROM customer WHERE user = '$username'");

while($row = mysql_fetch_array($result))
  {
$custemail = $row['email'];
  }

echo "Session username: " . $username . "";
echo "Session customer email: " . $custemail . "";
?>

 

The only errors that display are:

 

mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /test7.php on line 12

 

Undefined variable: custemail in /test7.php on line 18

 

I do have a record with the user field matching the session username, but the custemail is blank.

 

Thanks for the help.

i had to move the code around like this to make it work. thanks for the help.

 



<?php
ini_set('display_errors',1);
error_reporting(E_ALL|E_STRICT);

include ("include/session.php");
include ("include/connect.php");

$username = $session->userinfo['user'];


$result = mysql_query("SELECT * FROM customer WHERE user = '$username'");

while($row = mysql_fetch_array($result))
  {
$custemail = $row['email'];
  }

echo "Session username: " . $username . "";
echo "Session customer email: " . $custemail . "";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.