sanchez77 Posted November 18, 2010 Share Posted November 18, 2010 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 More sharing options...
Pikachu2000 Posted November 18, 2010 Share Posted November 18, 2010 "SELECT email FROM customer WHERE user = '$username'" should be just fine. What problems are you having? Link to comment https://forums.phpfreaks.com/topic/219029-mysql-query-with-a-php-variable/#findComment-1135878 Share on other sites More sharing options...
sanchez77 Posted November 18, 2010 Author Share Posted November 18, 2010 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 https://forums.phpfreaks.com/topic/219029-mysql-query-with-a-php-variable/#findComment-1135883 Share on other sites More sharing options...
harristweed Posted November 18, 2010 Share Posted November 18, 2010 are you sure you have a connection to the database? Link to comment https://forums.phpfreaks.com/topic/219029-mysql-query-with-a-php-variable/#findComment-1135887 Share on other sites More sharing options...
sanchez77 Posted November 18, 2010 Author Share Posted November 18, 2010 pretty sure, it's the same connection doc that i am using in the app Link to comment https://forums.phpfreaks.com/topic/219029-mysql-query-with-a-php-variable/#findComment-1135892 Share on other sites More sharing options...
sanchez77 Posted November 18, 2010 Author Share Posted November 18, 2010 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 https://forums.phpfreaks.com/topic/219029-mysql-query-with-a-php-variable/#findComment-1135899 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.