如何在php中回显搜索结果

时间:2022-09-10 19:18:18

I've been trying for days to create a simple search engine to search for information in my database. The table is myisam and I get search results when using mysql to search directly in phpmyadmin. So the problem seams to be with the PHP.

我一直在努力创建一个简单的搜索引擎来搜索我的数据库中的信息。该表是myisam,当我使用mysql直接在phpmyadmin中搜索时,我获得了搜索结果。所以这个问题与PHP有关。

When searching all I get is an empty page. I've tried serveral varieations of code I've found in online tutorials, but nothing seems to work. I hope there is a simple solution that I'm too dumb to see, and I hope someone can explain to me how to do this.

搜索所有我得到的是一个空页。我已经尝试了在在线教程中发现的几个代码变量,但似乎没有任何效果。我希望有一个简单的解决方案,我太愚蠢了,我希望有人可以向我解释如何做到这一点。

if(!empty($_POST['search'])){

    $search = $_POST['search'];

    $sqlString = "SELECT * FROM test WHERE MATCH (title, about) AGAINST ('$search')";
        $result = mysqli_query($dbLink, $sqlString) or die("Could not search.." . mysqli_error($dbLink));
        $row = mysqli_fetch_assoc($result);

    if($result-> num_rows > 0){
        while($row = mysqli_fetch_assoc($result)){
            $title = $row['title'];
            echo $title;
        }
    }else{
        echo 'No results';
    }   
}

2 个解决方案

#1


if(!empty($_POST['search'])){

    $search = $_POST['search'];

    $sqlString = "SELECT * FROM test WHERE MATCH (title, about) AGAINST ('$search')";
        $result = mysqli_query($dbLink, $sqlString) or die("Could not search.." . mysqli_error($dbLink));
        $row = mysqli_fetch_assoc($result); // remove it 

    if($result-> num_rows > 0){
        while($row = mysqli_fetch_assoc($result)){ // already exist
            $title = $row['title'];
            echo $title;
        }
    }else{
        echo 'No results';
    }   
}

#2


Change

if($result-> num_rows > 0)

To

if(mysqli_num_rows($result) > 0)

#1


if(!empty($_POST['search'])){

    $search = $_POST['search'];

    $sqlString = "SELECT * FROM test WHERE MATCH (title, about) AGAINST ('$search')";
        $result = mysqli_query($dbLink, $sqlString) or die("Could not search.." . mysqli_error($dbLink));
        $row = mysqli_fetch_assoc($result); // remove it 

    if($result-> num_rows > 0){
        while($row = mysqli_fetch_assoc($result)){ // already exist
            $title = $row['title'];
            echo $title;
        }
    }else{
        echo 'No results';
    }   
}

#2


Change

if($result-> num_rows > 0)

To

if(mysqli_num_rows($result) > 0)