如何在搜索中使用substr函数

时间:2016-04-26 11:25:50

标签: php html mysql sql search-engine

我需要使用substr来获取已写信的姓名,或者我是否写了名称的一部分

 if(isset($_GET['search'])){
 $sql="SELECT * FROM students WHERE first_name='".$_POST['sname']."' OR         last_name='".$_POST['fname']."'";
 $record=mysql_query($sql);}

<?php
echo"<form action='admin.php?page=3&search' method='Post'>";
echo"<input type='text' name='sname' value='ex: Student Name'> &nbsp; &nbsp;";
echo"<input type='text' name='fname' value='ex: Family Name'> &nbsp; &nbsp;";
echo"<button type='submit' name='search'   class='btn' >Search</button>";
echo"</form>";
?>

2 个答案:

答案 0 :(得分:1)

我认为您需要更改查询,在查询中使用LIKE

 $sql="SELECT * FROM students WHERE first_name like '%".$_POST['sname']."%' OR last_name like '%".$_POST['fname']."%' ";

答案 1 :(得分:1)

使用LIKE %

if(isset($_GET['search']))
{
 $sql="SELECT * FROM students WHERE first_name LIKE '%".$_POST['sname']."%' OR last_name LIKE '%".$_POST['fname']."%'";
 $record=mysql_query($sql);
}