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
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.

Link to comment
Share on other sites

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 . "";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.