0

目的は、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);
            }
        }
    }
}
}
?>
4

3 に答える 3

0

ループが1000回を超えて実行されると、ループが多すぎるとサイトのパフォーマンスが低下する可能性があるため、必要なループを追加する必要がないこのコードを使用してみてください。
余分な不要なループを取り除くことによってサイトの速度を落とさないようにしてください。常に簡単な方法を使用して、サーバーの損傷を少なくしたり、コストを下げたりします。$ phonenumbersが配列の場合:

$phonenumbers = imploade(",", $phonenumbers)
また

$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);
            }
        }
    }
}
?>
于 2013-01-09T08:05:32.317 に答える
0
$contactListString = implode("','", $phonenumbers);
$query = "SELECT * FROM my_table WHERE mobile IN ('$contactListString')";
于 2013-01-09T08:12:30.690 に答える
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, ',') + ");";

これは、電話番号がデータベースに整数として格納されていることを前提としています。それらが文字列として格納されている場合は、$construct 文字列を作成する場所でそれらを引用符で囲む必要があります。

SQL クエリは、「SELECT * FROM my_table WHERE mobile IN(12345,6789,28191);」のようになります。

于 2013-01-09T08:01:27.610 に答える