Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我将如何在MySQL中编写这个?_Php_Mysql_Arrays - Fatal编程技术网

Php 我将如何在MySQL中编写这个?

Php 我将如何在MySQL中编写这个?,php,mysql,arrays,Php,Mysql,Arrays,我想通过比较列列表和数组来搜索MySQL数据库。我将如何做到这一点,可能都在MySQL中 以下是我想在PHP中执行的操作: <?php $array = array("a", "b", "c", "d"); // 'c' doesn't exist in the database $result = $this->mysqli->query("Query"); // Result should be array("a", "b", "d") ?> 您可以使用 从中的位置

我想通过比较列列表和数组来搜索MySQL数据库。我将如何做到这一点,可能都在MySQL中

以下是我想在PHP中执行的操作:

<?php
$array = array("a", "b", "c", "d");
// 'c' doesn't exist in the database
$result = $this->mysqli->query("Query");
// Result should be array("a", "b", "d")
?>
您可以使用

从中的位置选择*
试试这个:

// The array
$array = array("a", "b", "c", "d");

// This takes the array and puts it into a string like "a,b,c,d"
$array_for_query = implode(',', $array);

$result = $this->mysqli->query("SELECT * FROM table WHERE the_letter IN ({$array_for_query})");

字母
分别替换为表名和列名。

不要忘记,在查询中不能信任用户数据。如果使用从用户收集的数据数组,请确保在将数据直接填充到查询中之前,使用mysqli_real_escape_字符串对其进行转义。这可以防止SQL注入攻击。这种事情在框架或ORM中非常容易。你为什么要直接使用
mysqli
这样做?
// The array
$array = array("a", "b", "c", "d");

// This takes the array and puts it into a string like "a,b,c,d"
$array_for_query = implode(',', $array);

$result = $this->mysqli->query("SELECT * FROM table WHERE the_letter IN ({$array_for_query})");