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

您可以在 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 回答
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 回答