【问题标题】:Correct usage of array values in WHERE clause在 WHERE 子句中正确使用数组值
【发布时间】:2014-04-09 05:19:44
【问题描述】:

我正在尝试使用 php 设置回复通知应用程序。 当网站访问者在网站上填写回复表并为讨论中的其他参与者添加标签(#Mike、#Wale)时,应用程序会提取标签并使用 preg_match_all() 函数来处理并最终提取用户名从哈希标签中提取,然后将其存储在一个数组中。

根据用户名数组中的值,应用程序应该遍历数据库中的用户表,提取与用户名匹配的电子邮件地址,然后向每个用户发送回复通知。

应该执行选择查询的应用程序部分抛出错误,我在下面突出显示: 警告:mysqli_error() 只需要 1 个参数,0 在 C:\wamp\www\lost_and_found\send_reply_notification.php 第 33 行给出。

看看我下面的代码:

 <?php
 ///////////////////extract matches into array//////////////////////////
 $data = "#MIKE The party will start at 10:30 pm and #John1975 run untill 12:30 am. Please #Emeka, could you inform #Joseph?";
 preg_match_all('/#[a-z0-9]+/i', $data, $match, PREG_PATTERN_ORDER);
$usernames=$match[0] ;
 /////////////convert array into a string using implode///////////////////
$newlist = implode( ",", $usernames );

$count = count($newlist);
/////////////////store username into array///////////
for ($i = 0; $i < $count; $i++) {

 preg_match_all('/#[a-z0-9]+/i', $newlist, $match, PREG_PATTERN_ORDER);
 $full_name=$match[0];}

////////////////////////Extract usernames/email pair from database////////////////
for ($i = 0; $i < $count; $i++)
    //dabase connection script
    $sql = 'SELECT * FROM users where username='. $full_name[$i];
    // submit the query and capture the result
    $result = $conn->query($sql) or die(mysqli_error());
    if ($result) 
        {
            //send mail query here.
        }   
?> 

SELECT 查询中的 WHERE 子句似乎不接受 '$full_name[$i]' 作为值输入。

我该如何解决这个问题,以便使用 WHERE 子句中数组中的值遍历用户表?

谢谢。

【问题讨论】:

  • 仍在学习,但您的正则表达式似乎只匹配小写字母。也许使用 a-zA-Z0-9 或仅使用 \w 作为单词字符?您可以在 (exploded_value_1, exploded_value_2) 中使用用户名执行一个 sql 查询,然后循环获取结果,而不是每次通过循环执行 1 个查询
  • @Robp 正则表达式中表达式后面的“i”使其不区分大小写。
  • 哦,对了...谢谢。我关于只执行 1 个 SQL 查询的说明仍然存在......

标签: php mysql preg-match-all


【解决方案1】:

在查询中为名称添加引号

$sql = 'SELECT * FROM users where username="'. $full_name[$i].'"';

并且不要忘记将代码放在{ } 中的for 循环中。

for ($i = 0; $i < $count; $i++)
{
    //dabase connection script
    $sql = 'SELECT * FROM users where username="'. $full_name[$i].'"';
    // submit the query and capture the result
    $result = $conn->query($sql) or die(mysqli_error());
    if ($result) 
        {
            //send mail query here.
        } 
}

【讨论】:

    【解决方案2】:

    无需循环,只需内爆数组并在查询中使用 WHERE

    $sql = 'SELECT * FROM users where username IN ("'.implode(",",$full_name).'"';

    【讨论】:

      【解决方案3】:
      $full_name = array();
      for ($i = 0; $i < $count; $i++) {
      
           preg_match_all('/[a-z]+/i', $newlist, $match, PREG_PATTERN_ORDER);
           $full_name=$match[0];
       }
      
       $count = count($full_name);
      

      您应该在循环外定义$full_name 并再次重新创建$count

      也将查询更改为

      $sql = 'SELECT * FROM users where username="'. $full_name[$i].'"';
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-28
        • 1970-01-01
        • 2011-09-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多