I'm having trouble using SELECT in mysql_query to retrieve one row of data

shampouya
shampouya used Ask the Experts™
on
I have a mysql table called "Users" with four columns. I have a php file that starts out by defining a session variable below. But then later on, when I try to select the specific row in the table "where Username=$username", I get the error message "Unknown column 'bob' in 'where clause'." But if I replace that with "where Username='bob'" all of a sudden it works. Am I not allowed to use a php variable like $username in my "where" search?

<?php
session_start();
$username = $_SESSION['username'];
?>


$result = mysql_query("SELECT * FROM Users WHERE Username=$username") or die(mysql_error());
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Fixer of Problems
Most Valuable Expert 2014
Commented:
Text values need to be single quoted.  Your query should be:

$result = mysql_query("SELECT * FROM Users WHERE Username='$username' ") or die(mysql_error());

Open in new window

Author

Commented:
ahh, silly mistake, thanks!
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
You're welcome!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial