php - 在 WHERE 子句中正确使用数组值

标签 php mysql preg-match-all

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

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

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

看看下面我的代码:

 <?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 子句中使用数组中的值遍历用户表?

谢谢。

最佳答案

在查询中为名称添加引号,例如

$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.
        } 
}

关于php - 在 WHERE 子句中正确使用数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22953411/

相关文章:

php - 在不发送的情况下测试 PHPMailer 用户名和密码,即检查连接

php - MySQL 枚举与整数

php - Preg 用于检查它发生了多少次

mysql - 从多个表中选择 using in statement 并显示所有内容,无论结果如何

php - 提取字符串中的每一次出现

php - 使用正则表达式从 HTML 文档中的链接中提取 URL

javascript - php bin2hex() 在 javascript 中解码

java - 如何知道应用程序用户是否离线android应用程序?

php - MySQL查询用PHP函数替换字段名

mysql - fatal error : Uncaught Error: Call to undefined function mysql_connect()