sg552
sg552

Reputation: 1543

Querying with array

I request data from my database and put it in array because I want to use it outside of loop. The problem is on my second query the query didn't work.

$cari2 = "SELECT name, data FROM `dns_rr` WHERE `zone` = '".$row['id']."' LIMIT 0, 30 ";
$keputusan2 = mysql_query($cari2);

while($row2 = mysql_fetch_array($keputusan2)) {
//echo $row2['name'];
$arrayData[] = $row2['data'];
$arrayName[] = $row2['name'];
}


Array output:
Array ( [0] => gumblari.cn [1] => gumblar.cn ) Array ( [0] => gumblaro.cn [1] => okla )

I also use implode before running the query:
$implodeName = implode(", ",$arrayName); $implodeData = implode(", ",$arrayData);

Implode output:
gumblaro.cnoklagumblari.cngumblar.cn

   $cari3 = "SELECT data, name FROM `dns_rr` WHERE data IN ($implodeData,      $implodeName)";
$keputusan3 = mysql_query($cari3);

while($row3 = mysql_fetch_array($keputusan3)) {

echo $row3['data'];
echo $row3['name'];

}   

Query no 2 ($cari3) give me this error:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in



Help me please. What I need is to use the data from first query outside while loop thats why I resorted to array. Thank you.

Upvotes: 0

Views: 92

Answers (1)

AlexanderZ
AlexanderZ

Reputation: 521

Ignoring the other problems with your script (such as SQL injection), the value of $keputusan2 is most likely a boolean because the query failed. You need to make sure the $cari3 is valid. My guess is that the two variables aren't quoted and are interpreted as SQL rather than values. You need to make sure the two arrays look like 'value1', 'value2'. You can do this using '"' . implode('", "', $array) . '"'; Basically, print out the query before executing it and see what is wrong. also, see what mysql_error() says.

Upvotes: 1

Related Questions