【问题标题】:PHP searching table then showing more then one resultPHP 搜索表然后显示多个结果
【发布时间】:2013-12-21 06:39:57
【问题描述】:

我正在尝试创建搜索,无论如何我的网站是:

mysite.com/?p=search&t=[SEARCH VARIABLE]

因此,就我而言,如果我的 url 是我希望它搜索我的帖子表并给我在表中存在 [SEARCH VARIABLE] 的结果。主要是我希望它检查这些列:标题、内容和作者。如果那里有任何[搜索变量],我希望它显示出来。

尝试 [只有 1 列] 有效,但没有给出所有结果。

<?php
if (isset($_GET['t']))
{
    $stmt = $dbh->prepare('SELECT * FROM posts WHERE `title` like :t');
    echo ' <h1>Search results for: '.$_GET['t'].'</h1> ';
    if (!$stmt->execute(array(':t' => $_GET['t'])))
    {
        exit('Could not exec query with param: '.$_GET['t']);
    }

    while($row = $stmt->fetch(PDO::FETCH_ASSOC))
    {
        echo '<font size="4pt">'.$row["title"].'</font><br>';
    }
}
?>

我的尝试与第一个相同,但检查内容字段。也不行。

<?php
if (isset($_GET['t']))
{
    $stmt = $dbh->prepare('SELECT * FROM posts WHERE `title` like :t AND `content` like :t;');
    echo ' <h1>Search results for: '.$_GET['t'].'</h1> ';

    if (!$stmt->execute(array(':t' => $_GET['t'])))
    {
        exit('Could not exec query with param: '.$_GET['t']);
    }

    while($row = $stmt->fetch(PDO::FETCH_ASSOC))
    {
        echo '<font size="4pt">'.$row["title"].'</font><br>
    }
}
?>

对于第一个,它只显示一个带有标题的字段。但我希望它显示任何带有单词的东西。例如,如果您搜索“hey”并有“hey theres apples”,我希望它显示 hey theres apples。

编辑:

<?php 
if (isset($_GET['t']))
{
    $stmt = $dbh->prepare('SELECT * FROM posts WHERE `title` like %:t%');

    echo ' <h1>Search results for: '.$_GET['t'].'</h1> ';

    while($row = $stmt->fetch(PDO::FETCH_ASSOC))
    {
        echo '<font size="4pt">'.$row["title"].'</font><br>';
    }
}
?>

仍然需要帮助。

【问题讨论】:

    标签: php search


    【解决方案1】:

    您需要在绑定参数时应用通配符%,而不是在查询中。尝试以下(对于第一个代码 sn-p):

    <?php
    if (isset($_GET['t']))
    {
        $stmt = $dbh->prepare('SELECT * FROM posts WHERE `title` like :t');
        echo ' <h1>Search results for: '.$_GET['t'].'</h1> ';
        if (!$stmt->execute(array(':t' => "%{$_GET['t']}%"))) // <---------- Added % here
        {
            exit('Could not exec query with param: '.$_GET['t']);
        }
    
        ...
    }
    ?>
    

    更新(对于具有相同值的or 条件):

    您不能在同一查询中两次使用相同的命名参数。请尝试以下操作:

    <?php
    if (isset($_GET['t']))
    {
        $stmt = $dbh->prepare('SELECT * FROM posts WHERE `title` like :t or content like :content');
        echo ' <h1>Search results for: '.$_GET['t'].'</h1> ';
    
        $param_array = array(':t' => "%{$_GET['t']}%", ':content' => "%{$_GET['t']}%")
        if (!$stmt->execute($param_array))
        {
            exit('Could not exec query with param: '.$_GET['t']);
        }
    
        ...
    }
    ?>
    

    【讨论】:

    • 我怎样才能在其中添加 OR content 呢?尝试将其更改为 $stmt = $dbh-&gt;prepare('SELECT * FROM posts WHERE title` like :t OR content like :t');` 虽然它只选择标题。
    • @user3124460,您不能在同一个查询中使用相同的命名参数两次。为content 使用不同的名称,并将相同的值分配给content 的参数。
    • 实际上我在同一个查询中使用它。 $stmt = $dbh-&gt;prepare('SELECT * FROM posts WHERE title` 喜欢 :t 或 content 喜欢 :t 或 content 喜欢 :t');`
    • @user3124460,感谢您指出这一点。当我不久前使用它时,这对我不起作用。可能是PHP版本。感谢您确认它有效,我需要尝试一下。 :)
    • 是的,我实际上认为它也不起作用。猜对了。
    【解决方案2】:

    当您使用LIKE 子句时:

    • 必须引用
    • 你应该正确使用%

    一个例子:

    SELECT * FROM `posts` WHERE `title` LIKE '%:t%' -- :t appears anywhere in title
    SELECT * FROM `posts` WHERE `title` LIKE ':t%'  -- :t is at beginning of title
    SELECT * FROM `posts` WHERE `title` LIKE '%:t'  -- :t is at the end of title
    

    【讨论】: