如何在php

时间:2015-05-12 12:04:51

标签: php search mysqli echo

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

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

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 个答案:

答案 0 :(得分:0)

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';
    }   
}

答案 1 :(得分:0)

更改

if($result-> num_rows > 0)

if(mysqli_num_rows($result) > 0)