将整数内嵌到mysql select语句中(结果重复)

时间:2012-03-29 21:55:19

标签: php mysql select where implode

我想破坏整数并将它们作为逗号分隔的字符串回显。我已经这么做了将近三个小时,我无法让它工作 我目前得到的输出是:

  

104104,105104,105,106

而不是:

  

104105106

我检查了php论坛,并说它使用mysql_fetch_assoc来避免这种情况,但它不起作用。如果有人有任何想法请帮助。我的PHP看起来像这样:

<?PHP

$user_name = "root";
$password = "";
$database = "testdb";
$server = "127.0.0.1";

$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);

if ($db_found) 
{
$SQL = "SELECT TableA.c1 FROM TableA ";
$result = mysql_query($SQL);
     while ($db_field = mysql_fetch_assoc($result)) 
     { 
     $itinarry[]= $db_field['c1'];
     $string = implode(',',$itinarry);
     echo $string;
     }
mysql_close($db_handle);

}
else  
{
    echo "Database NOT Found ";
    mysql_close($db_handle);        
 }

 ?>

2 个答案:

答案 0 :(得分:1)

它正在做的是

echo implode(",", array(104))

echo implode(",", array(104, 105))

echo implode(",", array(104, 105, 106))

结果是:

104104,105104,105,106

 $string = implode(',',$itinarry);
 echo $string;

超出while循环;

答案 1 :(得分:0)

将代码更改为:

while ($db_field = mysql_fetch_assoc($result)) 
 { 
 $itinarry[]= $db_field['c1'];
 }
 $string = implode(',',$itinarry);
echo $string;