简体   繁体   English

准备好的SQL LIKE语句未返回所有结果

[英]Prepared SQL LIKE statement not returning all results

I have a function to search my ingredient DB (error reporting removed for clarity): 我有一个搜索我的成分数据库的功能(为清楚起见删除了错误报告):

    $servername = "localhost";
    $username = "root";
    $password = "*****";
    $dbname = "addb_db";

    $query = "%" . $_POST["query"] ."%";

    $conn = new mysqli($servername, $username, $password, $dbname);

    $sql = "SELECT id, name FROM addb_ingredients WHERE name LIKE ? or id like ?";
    $stmt = $conn -> prepare($sql)
    $stmt->bind_param('ss', $query,$query)
    $stmt->execute()
    $stmt->bind_result($id,$name)
    while ($stmt->fetch()) {
        $ing_array[] = array($id,$name);
    }
    $response_array["status"] = "success";
    $response_array["data"] = $ing_array;

    echo json_encode($response_array);


    $stmt->close();
    $conn->close();

This only returns partial results for particular queries, eg for "bi", these are the results returned: 这仅返回特定查询的部分结果,例如“ bi”,这些是返回的结果:

  • Bison Grass Flavored Vodka 野牛草味伏特加
  • Bitter
  • Bitter Lemon 苦柠檬
  • Bitters 苦药类,浓生啤酒
  • Celery Bitters 芹菜苦酒
  • Chilled hibiscus tea 芙蓉花茶
  • Chocolate Bitters 巧克力苦味
  • Orange Bitters 橙色苦味
  • Peach Bitters 桃苦
  • Peychaud's bitters 佩乔德的苦涩
  • Wasabi Paste 芥末酱

Running this query on phpMyAdmin returns the same, as it should. 在phpMyAdmin上运行此查询将返回相同的结果。

However, when entering "bit", the results from the php above are: 但是,当输入“ bit”时,上面的php的结果是:

  • Bitter Lemon 苦柠檬
  • Bitters 苦药类,浓生啤酒
  • Celery Bitters 芹菜苦酒
  • Chocolate Bitters 巧克力苦味
  • Orange Bitters 橙色苦味
  • Peach Bitters 桃苦
  • Peychaud's bitters 佩乔德的苦涩

Whereas the results should be: 结果应该是:

在此处输入图片说明

The 'bitter' result is excluded and I can't work out why. “苦涩”的结果被排除在外,我不知道为什么。

I'm not particularly strong with PHP, but I've noticed that 'Bitter' would have been the first result if using '%bit%' as a parameter. 我对PHP并不是特别强壮,但是我注意到,如果使用'%bit%'作为参数,则'Bitter'将是第一个结果。 Likewise, 'Absolut Hibiskus' should be the first result when using '%bi%' as a parameter. 同样,将'%bi%'用作参数时, 'Absolut Hibiskus'应该是第一个结果。

My guess is that the first result is consumed within this block of code without actually being assigned anywhere within $ing_array : 我的猜测是,第一个结果是在此代码块中使用的,而没有实际分配给$ing_array任何位置:

$stmt->bind_result($id,$name)
while ($stmt->fetch()) {
    $ing_array[] = array($id,$name);
}

Quite indicative, there are no screenshots for the actual output provided. 完全表明,没有提供实际输出的屏幕截图。 Means the OP is judging their prepared statement output by distant indirect consequences. 表示OP正在根据遥远的间接结果来判断其准备好的语句输出。 Like some processing on the client side. 像客户端上的一些处理。

There are no errors neither in prepared statements nor in LIKE statement responsible for such a behavior. 在准备好的语句或LIKE语句中,没有任何错误负责这种行为。 100% of reported errors of the same kind were caused by the userland code. 报告的同类错误的100%是由用户域代码引起的。

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

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