使用Array的MySql查询

时间:2014-01-29 17:02:04

标签: php mysql arrays echo

我需要一些关于此代码的帮助:

<?php
include("config.php");

$Servico=implode(" ", $_POST['service']);

$con=mysqli_connect("","","","");  //I hid the info here

if (mysqli_connect_errno())
    {
        echo "Falhou a conexão ao MySQL: " . mysqli_connect_error();
    }

$to = mysqli_query($con,"SELECT Email FROM utilizadores WHERE Nome LIKE '%$Servico%'");
$row = mysqli_fetch_assoc($to);
$row['Email'];
foreach ($row as $destino)
    {
        echo $destino;
    }

?>

通过选择数据库中存在的一个或多个用户,可以在其他代码中创建数组。然后它会在选择用户的情况下创建数组。

当我的数组只有一个名字时,它会正确检索电子邮件,但当我有多个时,它会给我:

Warning: Invalid argument supplied for foreach() in /home/a5301436/public_html/criartarefa.php on line 49

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

以下是使用IN

的方法
$con=mysqli_connect("","","","");  //I hid the info here

if (mysqli_connect_errno())
{
    echo "Falhou a conexão ao MySQL: " . mysqli_connect_error();
}

$Servico=implode(", ", array_map(function($s) use ($con) {
    return "'" . mysqli_real_escape_string($con, $s) . "'";
    }, $_POST['service']));

$to = mysqli_query($con,"SELECT Email FROM utilizadores WHERE Nome IN ($Servico)");

while ($row = mysqli_fetch_assoc($to)){ 
    echo $row['Email'];
}