php - 奇怪的 MySQL 通配符行为

标签 php mysql wildcard

我在我的 PHP 代码中遇到了一种奇怪的行为,我一直无法弄清楚

使用此代码(暂时省略全部代码)-->

WHERE ss.showName LIKE '%".$searchFor."%' 

$searchFor 由用户输入定义并通过 AJAX 请求发送。结果作为 $tvLine 存储在 $result 中。然后用 $tvLine['showName'];

以 html 形式显示

现在假设我正在寻找 ss.showName“Joni and Friends”... 如果我输入“Joni and”中使用的任意数量的字母,我会收到正确的结果 -->“Joni and Friends” 同样,如果我输入“riends”中使用的任意数量的字母,我会收到正确的结果 -->“Jonie and Friends”。

但是,如果我只输入“F”或“f”,我不会收到任何结果。

这是怎么以及为什么会这样?

谢谢

if (  isset($_GET['userInput']))  {

        $searchFor = $_GET['userInput']; // the actual user search content
        $searchDate = $_GET['sDate']; // ascertains the correct day for search
        $searchTz = $_GET['sTz'];   // ascertains the correct timezone for search
        // conditional statement to check time difference for MySQL
            if ($searchTz == "UK"){ $searchTz = 0;} 
                else { $searchTz = 5; }

        global $wpdb; 
        $now = date("Y-m-d H:i:s");
        $params = array($now);
        $sql = "SELECT se.*, ss.showName, TIMEDIFF(NOW(), UTC_TIMESTAMP) as server_date
            FROM showpress_episodes 
            AS se LEFT JOIN showpress_shows AS ss 
            ON se.showId=ss.id 
            WHERE ss.showName LIKE '%".$searchFor."%' 
            AND DATE(episodeStartTime) = CURRENT_DATE() + INTERVAL $searchDate DAY
            AND episodeStartTime >= NOW() - INTERVAL $searchTz HOUR
            ORDER BY episodeStartTime 
            ";
        $result = $wpdb->get_results($wpdb->prepare($sql, $params), ARRAY_A);

?>
<h2>Search Results &nbsp; &nbsp; <a id="search_close"  onclick="closeForm()">X</a></h2>
<?php
// create table to hold SQL     
        if (count($result) < 1){
            echo ('<p>
                    <table border="0">
                    <tr><td>Show Name</td></tr>
                    <tr ><td colspan="3">NO RESULTS </td> </tr>
                    <tr><td>'.$searchFor.'</tr></td>
                    </table>');         
        }
        else{
            $display_string .= '<p>
                    <table border="0" id="ajax-table">';
                    //$display_string .= "<tr><td>".$searchFor."</tr></td>"; 
            foreach ($result as $tvLine) {

                $display_string .= "<tr><td>"; 
                //$display_string .= $tvLine[server_date];
                $display_string .= "<a id='". $tvLine[id] ." 'class='pointer search now-playing'>".$tvLine['showName']."</a>";
                $display_string .= "</td></tr>";

            }
            $display_string .= "</table></p>";
        }
        echo $display_string;

        exit();

最佳答案

您应该阅读:wpdb::prepare( string $query, mixed $args )

代码应该如下所示(未经测试,我没有可供测试的 WordPress):

    global $wpdb; 
    //$now = date("Y-m-d H:i:s");
    $params = array("%".$searchFor."%", $searchDate, $searchTz);
    $sql = "SELECT se.*, ss.showName, TIMEDIFF(NOW(), UTC_TIMESTAMP) as server_date
        FROM showpress_episodes 
        AS se LEFT JOIN showpress_shows AS ss 
        ON se.showId=ss.id 
        WHERE ss.showName LIKE %s 
        AND DATE(episodeStartTime) = CURRENT_DATE() + INTERVAL %d DAY
        AND episodeStartTime >= NOW() - INTERVAL %d HOUR
        ORDER BY episodeStartTime 
        ";
    $result = $wpdb->get_results($wpdb->prepare($sql, $params), ARRAY_A);

关于php - 奇怪的 MySQL 通配符行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59774002/

相关文章:

php - 当 xpath 在 xml 中找不到命名空间时避免错误

linux - rsync 通配符在 bash 脚本中不起作用

linux - 在 tox 命令中使用通配符

PHP_Doctrine 2.0 _连接管理

php - 我如何通过php spamhaus ip黑名单查询

javascript - 通过 ajax 登录和注册是否安全?

mysql - 我移动了我的 Joomla 网站,但现在我无法登录管理工具

php - 表 tr 未按预期正确替换

mysql - 将子查询列添加到复杂的选择查询

python - 通过匹配整数的第 n 位数字来返回 Pandas 数据帧中的行?