Chris Bennett
Chris Bennett

Reputation: 181

Unknown column in where clause

This script is supposed to retrieve the CustomerID for the Customer_First_Name and Customer_Last_Name that has been entered into a form.

$query  = "SELECT CustomerID FROM customer WHERE Customer_First_Name = `.$db_customer_first_name.` AND Customer_Last_Name = `.$db_customer_last_name.`";
$result = mysql_query($query)
or die(mysql_error());
echo $result;
echo $query;

when the script runs I get this error:

Unknown column '.Christopher.' in 'where clause'

the query is never printed on the screen.

any help is appreciated.

Upvotes: 0

Views: 184

Answers (4)

Tamil Selvan C
Tamil Selvan C

Reputation: 20199

Use ' instead of remove `

$query  = "SELECT CustomerID FROM customer WHERE Customer_First_Name = '".$db_customer_first_name."' AND Customer_Last_Name = '".$db_customer_last_name."'";

Upvotes: 0

Karl M.W.
Karl M.W.

Reputation: 747

This should work:

$query  = "SELECT CustomerID FROM customer WHERE Customer_First_Name = '$db_customer_first_name' AND Customer_Last_Name = '$db_customer_last_name'";

You need to use normal single quotes for values. Also you don't need to break the string if you're using double quotes - variables are detected within the string.

Make sure you're also correctly escaping your strings for mysql to prevent injection.

Better still, look at moving to mysqli and using prepared statements.

Upvotes: 0

medina
medina

Reputation: 8159

You have to use single quotes for strings. Try again:

$query  = "SELECT CustomerID FROM customer WHERE Customer_First_Name = '".$db_customer_first_name."' AND Customer_Last_Name = '".$db_customer_last_name."'";

Upvotes: 0

meda
meda

Reputation: 45490

Your quotes are bad use ' instead of the tick `

Upvotes: 1

Related Questions