Php 我将如何在MySQL中编写这个?
我想通过比较列列表和数组来搜索MySQL数据库。我将如何做到这一点,可能都在MySQL中 以下是我想在PHP中执行的操作: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") ?> 您可以使用 从中的位置
<?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})");