寻找类似SQL LIKE运算符的东西

时间:2013-07-16 18:23:57

标签: php

我正在寻找相同的代码来搜索我的表colmon中的东西。 我不希望它的搜索“喜欢”输入搜索。 我想让它在表中搜索确切的值。

如果用户搜索“行尸走肉的第3季第1集” 它在“moviename”排中完全搜索“行尸走肉的第3集第1集”。 并且没有告诉我所有结果看起来像那个..

我的代码现在是:

<?php
function connect($host,$username,$database,$password){
 $to_connect = mysql_connect($host,$username,$password) or die ("UNFinded ".$username. " DB !");
 $db = mysql_select_db($database, $to_connect) or die (mysql_error());
 return $db;
}
connect("localhost","idevice2_ariel","idevice2_ariel","ariel123456");


if (!isset($_POST['submit_form'])) {
    echo '<form name="search_form1" method="POST" action="search.php">
<input type="text" name="search_name" value="search movie name" />
<input type="submit" value="Submit" name="submit_form" />
</form>';
} else {
       $search_name = $_POST['search_name'];
       $query = mysql_query ("SELECT * FROM members where `moviename` = $search_name");
       $count = mysql_num_rows($query);
       while($row = mysql_fetch_array($query)) {
       $fname = $row['moviename'];
       $lname = $row['links'];
       print '<a href="'.$row['links'].'">'.$row['moviename'].'</a><br />';

}

}
?> 
谢谢你!爱丽儿。

1 个答案:

答案 0 :(得分:0)

if (!isset($_POST['submit_form'])) {
    echo '<form name="search_form1" method="POST" action="search.php">
<input type="text" name="search_name" value="search movie name" />
<input type="submit" value="Submit" name="submit_form" />
</form>';
} else {
       $mysqli = new mysqli('localhost', 'my_user', 'my_password', 'world');
       $search_name = $_POST['search_name'];
       $query = $mysqli->prepare("Select * from members where moviename = ? OR links = ?");
       $query->bindParam('ss',$search_name,$search_name);
       $query->execute();
       $query->bind_result($link,$movieName);
       while($query->fetch()) {
           echo '<a href="'.$link.'">'.$movieName.'</a><br />';    
       }
       $query->close();

}
?>

考虑使用预准备语句。