PHP在搜索时不会加载MySQL数据

时间:2017-04-03 14:15:00

标签: php mysql

我正在尝试在我的网站上创建一个搜索表单,其中打印出数据库中出现的所有可能的内容。然而,当我搜索某些东西时,它总是让我回想起它无法找到任何东西。我该如何解决这个问题?

PHP代码:

$host = 'localhost';
$user = '111042';
$password = 'jcbvrjd8';
$db_name = '111042';
$search = $_POST["search"];
if(isset($search)) {
    $db = mysqli_connect($host, $user, $password, $db_name);
    $wild_search = "%".$search."%";
    $findname = "SELECT `name`,`surname`
                FROM `Account` 
                WHERE `name` LIKE '".$wild_search."'
                    OR `surname` LIKE '".$wild_search."'
                    OR CONCAT(`name`, `surname`) LIKE '".$wild_search."'
                    OR CONCAT(`name`, `surnameprefix`, `surname`) LIKE '".$wild_search."';";
    $query = mysqli_query($db,$findname);
    $results = mysqli_fetch_all($query);

    if($result) {
        echo "<div id='searchresult'>\n";
        echo "<h1>People Found:</h1>\n";
        echo "<table id='searchresult'>\n";
        foreach($result as $rowno => $row) {
            echo "<tr class='searchtablerow'>\n";
            echo "<td>".$row['name'].", ".$row['surname']."</td>\n";
            echo "</tr>\n";
        }
        echo "</table>\n";
    } else {
        echo "<div id='searchresult'><h1>People Found:</h1>\n";
        echo "<p>No one was found...</p>\n";
        echo "</div>\n";
    }
} else {
}

HTML表单:

<div id="searchform">
    <h1>Search friends:</h1>
    <form name="searchform" method="post" action ="searchlink.php">
        <input type="text" name="search" id="search" autofocus placeholder="e.g. John Smith..."></input> <br>
        <input type="submit" name="submitsearch" value="Search" id="searchbutton"></input>
    </form>
</div>

谢谢你们帮助我。

PS。请不要烤我,我不像你那么先进。

1 个答案:

答案 0 :(得分:0)

$query = mysqli_query($db,$findname);
$results = mysqli_fetch_all($query);

if ($results) {
...

s ”在结果结束时丢失。 (或删除s中的$results = mysqli_fetch_all($query);