Domanda

Lets say I want to send an email to the people who like big sized candy.

When I do this:

$query1 = mysql_query("SELECT id_candy FROM `candylist` WHERE
candysize > 100") or die(mysql_error());

$result = mysql_fetch_array( $query1 );

$query2 = mysql_query("SELECT mail FROM `buyers` WHERE
candysizeinterest IN ($result)
") or die(mysql_error());

This query2 is of course the issue.

$result is not in a proper format to be used and then I get the obvious error of:

**Unknown column 'Array' in 'where clause'**

I need help to format the results of the query in a way that it looks like:

($val1, $val2, $val3....and so on) 

Then I will be able to use it for an IN statement.

But I just do not know how to do so.

È stato utile?

Soluzione 2

you have a mistake in your sintax of $query2 you place array instead of actual value

change

$query2 = mysql_query("SELECT mail FROM `buyers` WHERE candysizeinterest IN ($result)

to

$query2 = mysql_query("SELECT `mail` FROM `buyers` WHERE `candysizeinterest` IN ('".$result['id_candy']."')");


I would like to also to remember you that mysql_ functions are deprecated so i would advise you to switch to mysqli or PDO for new projects.

Altri suggerimenti

Try

$array = array();
while($result = mysql_fetch_array( $query1 ))
{
   $array[] = $result['id_candy'];
}

Then use implode

$in_condition  = implode(',', $array);

At last

$query2 = mysql_query("SELECT mail FROM `buyers` WHERE candysizeinterest IN ($in_condition)

NOTE : please switch to PDO or mysqli_*

You can use implode() to turn your array into a comma separated string:

implode(',',$result);

You can use that with IN

To convery an array to a comma sepearted string you would use

implode (",",$array)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top