如何将每个值从foreach循环传递给mysql查询

时间:2013-01-09 07:55:22

标签: php

目的是从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);
            }
        }
    }
}
}
?>

3 个答案:

答案 0 :(得分: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);”

答案 1 :(得分: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);
            }
        }
    }
}
?>

答案 2 :(得分:0)

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