2015-06-10 88 views
0

我想從MySQL數據庫的「簡單」數組中獲取列表或電子郵件,所以我可以在PHPmailer中使用它們發送電子郵件。 但是我得到多維數組,而不是數組:PHP使用mysqlli返回「簡單」數組

$query = "SELECT DISTINCT `EmailAddress` FROM `Emails` WHERE `JobID` = 1"; 
$result = $conn->query($query);  
if (!$result) { 
    printf("Query failed: %s\n", $mysqli->error); 
    exit; 
}  
while($row = $result->fetch_row()) { 
    $rows[]=$row; 
} 
$result->close(); 
$conn->close(); 
var_dump($rows); // This will return array(2) { [0]=> array(1) { [0]=> string(24) "[email protected]" } [1]=> array(1) { [0]=> string(17) "[email protected]" } } 
//This is how array should be 
$MyV = array("[email protected]","[email protected]"); 
var_dump($MyV); //this is an array I need to have: array(2) { [0]=> string(11) "[email protected]" [1]=> string(11) "[email protected]" } 

非常感謝!

回答

4

fetch_assoc

while($row = $result->fetch_assoc()) { 
    $rows[]=$row['EmailAddress']; 
} 
+0

非常感謝!完美運作 – Petrik

1

在循環中的這一點上,您可以訪問您正在討論的簡單數組。

while($row = $result->fetch_row()) { 
    $rows[]=$row; 
} 

所以,如果你想做的任何事情與陣列將其分配給某個變量或致電與陣列功能,是這樣的:

while($row = $result->fetch_row()) { 
    doSomethingFun($row); 
} 
1

上面的答案是正確的,但以下是對當前源進行最小代碼修改的示例。

while($row = $result->fetch_row()) { 
    $rows[]=$row[0]; 
} 

現在$rows將填入電子郵件地址一個維數組。