0

I'm trying to do this, but it returns null?

$query_1=$field_name[0]."='{".$field_value[0]."}'";

and then

getType = mysql_query("SELECT * FROM wines WHERE $query_1") or die(mysql_error());

while if i do like this:

 $getType = mysql_query("SELECT * FROM wines WHERE $field_name[0]='{$field_value[0]}'") or die(mysql_error());

it works fine.

is this even possible, or am I missing something too obvious? thank you in advance!

1
  • 1
    The solution is in the answers below, but you should really be aware that there is a high risk of SQL injection if you do it this way. Please sanitize your input before using it directly in a SQL query like this! Commented Nov 23, 2011 at 12:35

2 Answers 2

1

You are building it the wrong way. You should never use curly brackets (or any other string) in a SQL query. Concatenate your query instead.

Like this:

$query_1=$field_name[0]."='".$field_value[0]."'";

and oh, you missed a $ before your query, thats why its null.

Sign up to request clarification or add additional context in comments.

Comments

1

This works for me:

$field_name[0] = "test";
$field_value[0] = "someting";
$query_1=$field_name[0]."='".$field_value[0]."'";
echo ("SELECT * FROM wines WHERE $query_1") or die(mysql_error());

Hope it helps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.