简体   繁体   English

MySQL之类的查询无法正常工作?

[英]MySQL like query not working?

I have a successful connection made in PDO to my MySQL database and I am currently trying to get it to query the database for itmes LIKE the search query. 我已经在PDO中成功建立了与MySQL数据库的连接,而我目前正试图使其在数据库中查询类似于搜索查询的itmes。

<?php
include ('connection.php');

function doSearch() {
    $output = '';
    if(isset($_POST['search'])) {
        $searchq = $_POST['search'];
        $searchq = preg_replace ("#[^0-9a-z]#i","",$searchq);
    $sql = "SELECT * FROM entries WHERE name LIKE :searchq or description LIKE :searchq or content LIKE :searchq";

    global $conn;
    $stmt = $conn->prepare($sql);
    $stmt->bindParam(":searchq",$searchq,PDO::PARAM_STR);
    $stmt->execute();
    $count = $stmt->rowCount();
 if($count == 0) {
            $output = 'No results found, buddy.';
        } else {
            while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                $eName = $row['name'];
                $eDesc = $row['description'];
                $eCont = $row['content'];
                $id = $row['id'];
                $elvl = $row['level'];
                $ehp = $row['hp'];

                $output .= '<tr><td><a href="http://ccc.aaaa.whatever/' .$eName. '" onclick="document.linkform.submit();">'.$eName.'</a></td><td>'.$eDesc.'</td><td>'.$elvl.'</td><td>'.$ehp.'</td></tr>';
            }
        }            

    return $output;
    }
 }
?>

I am struggling to get it to search. 我正在努力寻找它。 Unless the query exactly matches only the name, it doesn't show any results. 除非查询仅与名称完全匹配,否则它不会显示任何结果。

The LIKE operator does not do partial matches unless specifically instructed to. 除非明确指示,否则LIKE运算符不会进行部分匹配。 Perhaps you meant to prepend/append a % wildcard symbol to the search string: 也许您打算将%通配符添加/添加到搜索字符串中:

$searchq = '%' . $searchq . '%';

I don't see anything wrong in the SQL text of the query. 我没有在查询的SQL文本中看到任何错误。

The simplest explanation is that the SQL you are expecting to be sent to the database isn't what is being sent. 最简单的解释是,您期望发送到数据库的SQL不是正在发送的SQL。

I suggest you give this a try: 我建议您尝试一下:

use unique bind variable names in the statement, use each bind variable only once. 在语句中使用唯一的绑定变量名称,每个绑定变量只能使用一次。

There used to be a bug in PDO with named bind variables (not sure if that's fixed of not. Under the covers, the named bind parameters were getting converted to positional notation, and when the same bind variable two or more times, the query being sent to MySQL wasn't what we expected. 在PDO中曾经有一个带有命名绑定变量的错误(不确定是否已解决。)在幕后,命名绑定参数已转换为位置表示法,并且当同一绑定变量两次或更多次时,查询被发送到MySQL并不是我们期望的。

For example: 例如:

 $sql = "SELECT e.* 
           FROM entries e
          WHERE e.name        LIKE :searchq1
             OR e.description LIKE :searchq2
             OR e.content     LIKE :searchq3";

$stmt = $conn->prepare($sql);
$stmt->bindParam(":searchq1",$searchq,PDO::PARAM_STR);
$stmt->bindParam(":searchq2",$searchq,PDO::PARAM_STR);
$stmt->bindParam(":searchq3",$searchq,PDO::PARAM_STR);

When we encountered the problem, we weren't using server side prepared statements, just regular client side prepares; 遇到问题时,我们不是在使用服务器端准备好的语句,而只是通过常规的客户端准备; the SQL sent to the server included literals, not placeholders. 发送到服务器的SQL包含文字,而不是占位符。 Turning on the general_log in MySQL allowed us to see the actual SQL statements that were being sent to the database. 在MySQL中打开general_log,使我们可以查看发送到数据库的实际SQL语句。

You might be encountering the same problem. 您可能会遇到相同的问题。 But I'd recommend this as a step in debugging the problem, at least to verify it isn't the problem. 但是,我建议您将此步骤作为调试问题的步骤,至少要验证这不是问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM