2012-06-25 67 views
0

在下面的示例中,我将ID的数组仅用于将其变成一段时间,然后返回到数组中,仅用于使页面上的逗号发生爆炸。当然,必须有一个更简单的方法来做到这一点。PHP mySQL查询删除双数组

我是$行[ 'ID']组成的数组后

function get_other_elector_phone($telephone,$current,$criteria){ 
    $the_others = mysql_query("SELECT * FROM electors WHERE telephone = '$telephone' $criteria"); $results = ''; 
    while($row = mysql_fetch_array($the_others)) { 
    $results .= $row['ID'].','; } return $results; 
} 


$others = get_other_elector_phone(g('electors',$elector,'telephone'),$elector,$criteria); 
        if($others){ $others = explode(',',$others); 
+2

只是...建立并返回一个数组而不是一个字符串? – DaveRandom

回答

-1

,而不是

function get_other_elector_phone($telephone,$current,$criteria){ 
    $the_others = mysql_query("SELECT * FROM electors WHERE telephone = '$telephone' $criteria"); $results = ''; 
    while($row = mysql_fetch_array($the_others)) { 
    $results .= $row['ID'].','; } return $results; 
    } 
} 

只是返回的mysql_fetch_array()

function get_other_elector_phone($telephone,$current,$criteria){ 
    $the_others = mysql_query("SELECT * FROM electors WHERE telephone = '$telephone' $criteria"); $results = ''; 
    return mysql_fetch_array($the_others); 
} 

的结果则有不需要爆炸函数的输出。

+0

如果您只想使用ID列,则使用'SELECT ID ...'而不是'SELECT * ...'可以获得更多的结果,而不仅仅是ID列 –

+0

。 –

+0

这只返回一个不是数组的项目,所以不好。 – DominicM

0

就像@daverandom说,刚重建阵列,这是语法:

function get_other_elector_phone($telephone,$current,$criteria){ 
    $the_others=mysql_query("SELECT * FROM electors WHERE telephone = '$telephone' $criteria"); 
    $results = array(); 
    $i=0; 
    while($row = mysql_fetch_array($the_others)) { 
    $results [$i]= $row['ID']; 
    $i++; 
    } 
    //results returned outside loop 
    return $results; 
} 

$others = get_other_elector_phone(g('electors',$elector,'telephone'),$elector,$criteria); 

的$其他人将返回一个数组。

+0

这只是返回第一个结果,也不是我所有的结果 –

+0

在这个例子中,'return $ results'需要在while循环之外。你也可以省去'$ i';赋值'$ results [] = $ row ['ID']'会做同样的事情。 –

+0

谢谢@joe bowman,我的意思是说,我只是放错了地方 – mo3lyana