将数组传递给字符串不行

时间:2013-05-09 22:55:10

标签: php mysql arrays php-5.3

我有这个脚本,我有一个问题将此数组传递给逗号的字符串。 我希望它是一个字符串,所以我可以将它作为变量插入select ... where ... IN($ variable); 我尝试了内爆,并且它不会回显ids作为一个字符串它回应数组。

error_reporting(E_ALL);
mysql_connect("localhost", "root", "root");
mysql_select_db("wall");

$sql = "select id from table where id=1";
$result = mysql_query( $sql);

$myArray='';
while($row = mysql_fetch_array($result)){
    $popurl = $row['id'];
    $myArray[] = $popurl;
}

echo "<pre>";
print_r($myArray);

1 个答案:

答案 0 :(得分:0)

试试这段代码:

$myArray= array() ; //Here you must declare it as array
while($row = mysql_fetch_array($result)){

  $popurl = $row['id'];

  $myArray[] = $popurl;
}

$string = "'" . implode("', '", $myArray) . "'" ; 
//Will make ready string like 'data','data2', 'data3'