2013-01-09 22 views
0

目標是從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); 
      } 
     } 
    } 
} 
} 
?> 
+0

不幸的是我不明白這個問題。 $ phonenumber在您的程序中是否爲空?你也可以過濾它來防止SQL注入,並使用mysqli而不是mysql。 – Ynhockey

+0

您是否嘗試過使用var_dump($ phonenumbers)來查看裏面的內容?一些說法:不要使用mysql_query,它已被棄用。此外,您應該確保將變量保存在字符串外部,使用printf或管理報價。 – Tikkes

+0

@Ynhockey我收到一個數組,當我打印$ phonenumber,我不能用它來立即查詢數據庫 – Marcus

回答

0
$contactListString = implode("','", $phonenumbers); 
$query = "SELECT * FROM my_table WHERE mobile IN ('$contactListString')"; 
+0

現在告訴我爲什麼使用「','」,將添加','作爲分隔符,並且不會工作在條款... –

+0

請檢查我的朋友,我認爲它必須在那裏。 –

+0

我建議你參考[IMPLODE](http://php.net/manual/en/function.implode.php)。 –

0

您可以在SQL查詢中使用IN關鍵字。所以你的代碼看起來像

$construct = "SELECT * FROM my_table WHERE mobile IN ("; 
if (!empty($phonenumbers)) { 
    foreach ($phonenumbers as $phonenumber) { 
     $construct = $construct + $phonenumber + ','; 
    } 
    //strip the trailing comma and close IN() statement 
    $construct = rtrim($construct, ',') + ");"; 

這是假設你的電話號碼存儲爲數據庫中的整數。如果它們存儲爲字符串,則必須在構建$構造字符串的位置將它們用引號括起來。 SQL查詢看起來像「SELECT * FROM my_table WHERE mobile IN(12345,6789,28191);」

+0

謝謝Jarriet, – Marcus

0

嘗試使用此代碼,您不需要添加nu必要的循環,因爲循環太多可能會降低站點性能,如果循環運行超過1000次。
儘量不要放慢你的網站被甩掉多餘的不必要的循環總是用簡單的方式在低服務器損壞達到的結果或減緩成本.. 如果$ PHONENUMBERS是數組,那麼:

$phonenumbers = imploade(",", $phonenumbers)
OR

$phonenumbers = str_replace(';',',', $to); 

然後

$construct = "SELECT * FROM my_table WHERE mobile IN ($phonenumbers)";

終極密碼必須

<?php 
if (!empty($_POST)) 
{ 
    $text = $_POST['message']; 
    $to = $_POST['recipients']; 
    include('mysql_connect.php'); 
    mysql_select_db("my_database"); 

    $to = explode(", ",$to); 
    $to = implode(" ",$to); 

    if ($to != '' && $text != "") 
    { 
     $phonenumbers = explode(';', $to); 
     $phonenumbers = implode(", ", $phonenumbers); 

     if (!empty($phonenumbers)) 
     { 

      $construct = "SELECT * FROM my_table WHERE mobile IN ("'.$phonenumbers.'")"; 
      $check = mysql_query($construct); 
      while($row = mysql_fetch_array($check)) 
      { 
       $name = $row[recipient_name]; 
      } 
      $filename = "/send_message"; 
      $handle = fopen($filename .".LOCK", "x+"); 
      if ($handle != false) 
      { 
       fwrite($handle, "To: $phonenumber\n"); 
       $text = "$name".$_POST['message']; 
       fwrite($handle, $text); 
       fclose($handle); 
      } 
     } 
    } 
} 
?>
+0

$ phonenumbers是數組 –

+0

所以使用implode,將解決問題 –

+0

他已經爆炸和imploding我不知道爲什麼順便說一句$ phonnumbers不是一個因爲他正在爆炸;這個。 –

相關問題