Hi there,

I want to check if a check if a certain value is in array with a row from my database, I think it is something like this but this code doesn’t give the right result, the value is never in array:

$sql = mysql_query("SELECT Row FROM table WHERE Type='Type'");
while ($row = mysql_fetch_array($sql)) {

$array = array("'".$row['Row']."', ");
if (in_array("Value", $array, true)) {
    // positive
   // negative

Hope you guys know what’s wrong about it.

Have you tried in_array without the third parameter?

Well the thing is that if I print

"'".$row['Row']."', "

and place that manually in the array the whole script does actually work which seems very weird to me.


I think that your problem may lie in the quotes. I see you have about 4 sets within the same array, making it very confusing just to look at. I would suggest escaping the quotes that are inside.

This mearly is just a suggestion though, being that I’m a newbie. :P
Hope you work things out!

-Teh Riddler

Ah, now I see the problem: you’re overwriting $array with a new, clean array each time you loop through the result resource. Try something like this:

$array = array();
$i = 0;
while ($row = mysql_fetch_array($sql)) {
$array[$i++] = $row[‘Row’];

Then it should work.


It works fine, much appreciated :D .


Sponsor our Newsletter | Privacy Policy | Terms of Service