building a sql query string with php parameters

791

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!

5

Answer

Solution:

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.

285

Answer

Solution:

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

People are also looking for solutions to the problem: Building custom PHP extension (.so)

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.