Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.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_Search_Pdo - Fatal编程技术网

Php 不需要在MySQL中进行精确搜索

Php 不需要在MySQL中进行精确搜索,php,mysql,search,pdo,Php,Mysql,Search,Pdo,我试图在PHP中创建一个不需要精确搜索的搜索函数,即如果搜索“mar”,它将返回“mark”和“mary” 到目前为止,我有下面的查询,但它没有达到我的预期 $query = $this->db->prepare("SELECT * FROM `visitors` WHERE `first_name` LIKE ? OR `last_name` LIKE ? OR `phone` LIKE ? OR `email` LIKE ? or `spouse` LIKE ? OR `spou

我试图在PHP中创建一个不需要精确搜索的搜索函数,即如果搜索“mar”,它将返回“mark”和“mary”

到目前为止,我有下面的查询,但它没有达到我的预期

$query = $this->db->prepare("SELECT * FROM `visitors` WHERE `first_name` LIKE ? OR `last_name` LIKE ? OR `phone` LIKE ? OR `email` LIKE ? or `spouse` LIKE ? OR `spousephone` LIKE ? OR `spouseemail` LIKE ? OR `child1name` LIKE ? OR `child2name` LIKE ? OR `child3name` LIKE ?");

这是我创建的函数的完整代码,也许有人可以建议正确的编写方法,因为我是PHP和MySQL的noob

    public function visitor_search($search_query) {

    $query = $this->db->prepare("SELECT * FROM `visitors` WHERE `first_name` LIKE ? OR `last_name` LIKE ? OR `phone` LIKE ? OR `email` LIKE ? or `spouse` LIKE ? OR `spousephone` LIKE ? OR `spouseemail` LIKE ? OR `child1name` LIKE ? OR `child2name` LIKE ? OR `child3name` LIKE ?");

    $query->bindValue(1, $search_query);
    $query->bindValue(2, $search_query);
    $query->bindValue(3, $search_query);
    $query->bindValue(4, $search_query);
    $query->bindValue(5, $search_query);
    $query->bindValue(6, $search_query);
    $query->bindValue(7, $search_query);
    $query->bindValue(8, $search_query);
    $query->bindValue(9, $search_query);
    $query->bindValue(10, $search_query);

    try {
        $query->execute();
    } catch(PDOException $e){
        die($e->getMessage());
    }

    return $query->fetchAll();
}

只要不向输入参数添加通配符,
LIKE
不会自动表现出与
=
不同的行为

以下查询将只查找
string
等于“Hello”的行:

以下查询将查找
string
包含“Hello”的行:

由于您没有包括实际执行查询的代码,我只能假设您没有添加那些
%
s


请参阅:

发生的是,您应该将%作为后缀或前缀附加。 在准备好的查询中?将被var内容替换,如果添加文本“”。要解决此问题,您应该在发送var以查询%chars之前添加

请检查:

$search = '%'.$_GET['search'].'%';    
$query = $this->db->prepare("SELECT * FROM `visitors` WHERE `first_name` LIKE ? OR `last_name` LIKE ? OR `phone` LIKE ? OR `email` LIKE ? or `spouse` LIKE ? OR `spousephone` LIKE ? OR `spouseemail` LIKE ? OR `child1name` LIKE ? OR `child2name` LIKE ? OR `child3name` LIKE ?");

结果=$query->execute(数组($search、$search、$search、$search、$search、$search、$search))

别忘了转义特殊字符,
\
%

即使使用预先准备好的语句,也必须避免这些字符。
(这意味着您必须在没有准备语句的情况下转义两次)

例如:

$stmt = $pdo->prepare('SELECT * FROM table WHERE foo LIKE ? AND bar LIKE ?');
$params = array(
    '%' . addcslashes($foo, '\\_%') . '%',
    '%' . addcslashes($bar, '\\_%') . '%',
);
$stmt->execute($params);
附言


您应该从外部捕获异常。

您需要将每个
的值包装在
%mar%
中,或者如果您只想匹配start with,则只包装
mar%
。您是对的,但在这种情况下,他使用的是参数查询。当然,问题仍然是一样的。我只是想通过使用纯SQL作为一个例子来保持它的简单性。为什么我会收到负数?这有一个问题:
'%.$\u GET['search'].%'
$search = '%'.$_GET['search'].'%';    
$query = $this->db->prepare("SELECT * FROM `visitors` WHERE `first_name` LIKE ? OR `last_name` LIKE ? OR `phone` LIKE ? OR `email` LIKE ? or `spouse` LIKE ? OR `spousephone` LIKE ? OR `spouseemail` LIKE ? OR `child1name` LIKE ? OR `child2name` LIKE ? OR `child3name` LIKE ?");
$stmt = $pdo->prepare('SELECT * FROM table WHERE foo LIKE ? AND bar LIKE ?');
$params = array(
    '%' . addcslashes($foo, '\\_%') . '%',
    '%' . addcslashes($bar, '\\_%') . '%',
);
$stmt->execute($params);
public function visitor_search($search_query) {
    $sql = "SELECT * FROM `visitors` WHERE `first_name` LIKE ? OR `last_name` LIKE ? OR `phone` LIKE ? OR `email` LIKE ? or `spouse` LIKE ? OR `spousephone` LIKE ? OR `spouseemail` LIKE ? OR `child1name` LIKE ? OR `child2name` LIKE ? OR `child3name` LIKE ?";
    $stmt = $this->db->prepare($sql);
    $params = array_fill(0, substr_count($sql, '?'), '%' . addcslashes($search_query, '\\_%') . '%');
    $stmt->execute($params);
    return $stmt->fetchAll(PDO::FETCH_ASSOC);
}