PHP mySQL查询删除双数组
在下面的示例中,我使用了一个ID数组,只是将其转换为一段时间,然后返回到一个数组,只是在页面上的逗号处将其分解。当然,一定有更简单的方法 我在寻找一个$row['ID']数组PHP mySQL查询删除双数组,php,mysql,arrays,Php,Mysql,Arrays,在下面的示例中,我使用了一个ID数组,只是将其转换为一段时间,然后返回到一个数组,只是在页面上的逗号处将其分解。当然,一定有更简单的方法 我在寻找一个$row['ID']数组 function get_other_elector_phone($telephone,$current,$criteria){ $the_others = mysql_query("SELECT * FROM electors WHERE telephone = '$telephone' $criteria");
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);
而不是
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\u fetch\u array()的结果
这样就不需要分解函数的输出。正如@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);
$others将返回一个数组。只需。。。构建并返回一个数组而不是一个字符串?有点问题,因为它得到的结果远远不止是ID列。如果您只需要ID列,请使用
SELECT ID…
而不是SELECT*。
。这只是返回第一个结果,也不是像我在本例中所做的那样返回所有结果,return$results
需要在循环之外。您也可以省去$i
;分配$results[]=$row['ID']
也会这样做。谢谢@joe bowman,我的意思是,我放错地方了
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);