PHP:无法从MYSQL数据库中获得完全匹配

时间:2016-02-09 13:00:30

标签: php mysql

这个php真的给了我一些问题。我想回收数据库中的数据,我希望它显示数据库中的主要数据,我希望它显示来自数据库的数据,以匹配从数据库输入的信息,例如,如果我输入名称“保罗Mason“并单击搜索按钮,它连接到数据库并回显站点上的信息。

我在下面编写了代码。

<html>
<title>Search Records</title>
<head>
<body>
    <form name="" id="" method="post" action="search.php"/>
    <p> Enter Student name : <input type="text" name="fullname" id="fullname"/>
    <input type="submit" name="senda" value="Search Data" />

</form>

<?php
if(isset($_POST['senda'])){

    include 'mysqlconn.php';
    $con = mysqli_connect($host, $dbuser, $pass, $db) or die('Cannot Connect');

    $name = $_POST['fullname'];

        $sql = "SELECT * FROM scores WHERE MATCH(fullname) AGAINST('$name')";
        $result = mysqli_query($con,$sql)  or die("Error: ".mysqli_error($con));

    while($row = mysqli_fetch_array($result,MYSQLI_ASSOC))
    {
        printf("%s (%s)\n", $row['Fullname'] ." ". $row['studentNo'] ." ". $row['SubjectName'] ." ". $row['GPA'] ." ". $row['CGPA'] ." ". $row['SCORE']);

        mysqli_free_result($result);
    }
     mysqli_close($con);
    }

?>
</body>
</head>
</html>

相反,它会显示其他类似内容:Error: Can't find FULLTEXT index matching the column list

什么是错的,我需要有人以编程方式纠正我!

1 个答案:

答案 0 :(得分:1)

您引用的列上没有FULLTEXT索引。对于单列搜索,如果您不想创建索引,请尝试(也为您转换为准备好的语句):

松散匹配:

$con = new mysqli($host, $dbuser, $pass, $db) or die('Cannot Connect');
$name = $_POST['fullname'];
$query = $con->prepare("SELECT * FROM scores WHERE fullname LIKE '%$name%'");
$query->execute();

完全匹配:

$con = new mysqli($host, $dbuser, $pass, $db) or die('Cannot Connect');
$name = $_POST['fullname'];
$query = $con->prepare("SELECT * FROM scores WHERE fullname = '$name'");
$query->execute();

如果你有或创建了一个FULLTEXT索引,那么它会起作用,但仍会建议准备查询以防止注入攻击:

$con = new mysqli($host, $dbuser, $pass, $db) or die('Cannot Connect');
$name = $_POST['fullname'];
$query = $con->prepare("WHERE MATCH(fullname) AGAINST('$name')");
$query->execute();