0

在下面的示例中,我采用了一个 ID 数组,只是为了将其转换为一段时间,然后再返回为一个数组,只是为了在页面上的逗号上将其分解。当然,必须有一种更简单的方法来做到这一点。

我在 $row['ID']s 的数组之后

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);
4

2 回答 2

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); 

$others 将返回一个数组。

于 2012-06-25T10:51:03.307 回答
-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);
} 

那么就不需要分解函数的输出了。

于 2012-06-25T10:41:23.700 回答