目標是從foreach循環中獲取每個值(電話號碼)並使用它查詢mysql數據庫,以便我還可以獲取與該數字對應的名稱如何將foreach循環中的每個值傳遞給mysql查詢
<?php
if (!empty($_POST)) {
$text = $_POST['message'];
$to = $_POST['recipients'];//this is an array
include('mysql_connect.php');//connect to my database
mysql_select_db("my_database");
$to = explode(", ",$to);
$to = implode(" ",$to);
if ($to != '' && $text != "") {
$phonenumbers = explode(';', $to);
if (!empty($phonenumbers)) {
foreach ($phonenumbers as $phonenumber) {;
$construct = "SELECT * FROM my_table WHERE mobile='$phonenumber'";//this is where my problem is, $phonenumber!!
$check = mysql_query($construct);
while($row = mysql_fetch_array($check)){
$name = $row[recipient_name];}//My aim is to use this name in the message body
$filename = "/send_message";//keep all messages in this file
$handle = fopen($filename .".LOCK", "x+");
if ($handle != false) {
fwrite($handle, "To: $phonenumber\n");
$text = "$name".$_POST['message'];//Every message should start with recipient name
fwrite($handle, $text);
fclose($handle);
}
}
}
}
}
?>
不幸的是我不明白這個問題。 $ phonenumber在您的程序中是否爲空?你也可以過濾它來防止SQL注入,並使用mysqli而不是mysql。 – Ynhockey
您是否嘗試過使用var_dump($ phonenumbers)來查看裏面的內容?一些說法:不要使用mysql_query,它已被棄用。此外,您應該確保將變量保存在字符串外部,使用printf或管理報價。 – Tikkes
@Ynhockey我收到一個數組,當我打印$ phonenumber,我不能用它來立即查詢數據庫 – Marcus