PHP字符串在多表列中比较和搜索
这是我的第一篇文章,我对PHP搜索有一个问题 这里,PHP字符串在多表列中比较和搜索,php,mysql,search,Php,Mysql,Search,这是我的第一篇文章,我对PHP搜索有一个问题 这里, $searchq = $_POST['searchq']; <pre> $searchq = $_POST['searchq']; $conn = mysqli_connect('localhost','root','','std_info') or die("Cant' Connect to db"); $query = mysqli_query($conn,"select * from student_details
$searchq = $_POST['searchq'];
<pre> $searchq = $_POST['searchq'];
$conn = mysqli_connect('localhost','root','','std_info') or die("Cant' Connect to db");
$query = mysqli_query($conn,"select * from student_details where first_name like '%$searchq%' or last_name like '%$searchq%'");
$count = mysqli_num_rows($query);
if($count == 0) {
echo "<br>";
echo "Can't find, try entering only first name or last name";
}
else {
do something`</pre>
}
到目前为止,当为searchq(如naresh、google、lamgade)提供一个单词时,它会在db中搜索,但当有多个搜索时,如
naresh lamgade
同时,这些单词也有错误,因为它只在名字列中搜索,我想在first\u name
列中搜索naresh,在last\u name
列中搜索lamgade
这是密码
$searchq=$\u POST['searchq'];
$conn=mysqli_connect('localhost','root','std_info')或die('Cant'connect to db');
$query=mysqli_query($conn,“从学生_详细信息中选择*,其中名为“%$searchq%”或姓为“%$searchq%”);
$count=mysqli\u num\u行($query);
如果($count==0){
回声“
”;
echo“找不到,请尝试只输入名字或姓氏”;
}
否则{
做点什么`
}
问题是
在搜索栏中,当我尝试输入naresh lamgade并搜索时
<?php
$searchq = explode(" ", $_POST['searchq']);
$conn = mysqli_connect('localhost', 'root', '', 'std_info') or die("Cant' Connect to db");
$query = mysqli_query($conn, "select * from student_details where (first_name like '%" . mysqli_real_escape_string($searchq[0]) . "%' OR first_name like '%" . mysqli_real_escape_string($searchq[1]) . "%') OR (last_name like '%" . mysqli_real_escape_string($searchq[1]) . "%' OR last_name like '%" . mysqli_real_escape_string($searchq[0]) . "%'");
$count = mysqli_num_rows($query);
if ($count == 0)
{
echo "
";
echo "Can't find, try entering only first name or last name";
}
else
{
do something`
searchq=naresh+lamgade
它使用naresh+lamgade
在first_name和last_name列中搜索,因此没有结果
我想知道,如何打断这两个单词,并用这些单词在不同的列中搜索。使用explode拆分查询。 你的代码也很危险。使用mysqli\u escape\u real\u字符串对查询中的特殊字符进行转义:
searchq =naresh+lamgade"
问题是
在搜索栏中,当我尝试输入naresh lamgade并搜索时,
<?php
$searchq = explode(" ", $_POST['searchq']);
$conn = mysqli_connect('localhost', 'root', '', 'std_info') or die("Cant' Connect to db");
$query = mysqli_query($conn, "select * from student_details where (first_name like '%" . mysqli_real_escape_string($searchq[0]) . "%' OR first_name like '%" . mysqli_real_escape_string($searchq[1]) . "%') OR (last_name like '%" . mysqli_real_escape_string($searchq[1]) . "%' OR last_name like '%" . mysqli_real_escape_string($searchq[0]) . "%'");
$count = mysqli_num_rows($query);
if ($count == 0)
{
echo "
";
echo "Can't find, try entering only first name or last name";
}
else
{
do something`
... WHERE first_name LIKE "'%'.$searchq.'%'" or last_name like "'%'.$searchq.'%');
我猜您将textfield放在表单中,而没有method=“post”
如果是,请在searchq中尝试以下操作:
$query = mysqli_query($conn, "SELECT * FROM student_details WHERE CONCAT(first_name,' ',last_name) like '%$searchq%'");
谢谢大家的回答,但我已经使用了这个查询,它的工作完全符合我的要求
使用“分解”功能分解搜索词。这将是一个开始。大概是这样的: