2012-06-25 66 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