php在msql上搜索多个字段并返回准确答案
我想我可能会改变问题的解释,因为没有任何帮助,但我真的需要帮助。 我的表单中有一个搜索框,允许用户在mysql表中搜索学生数据,我只成功搜索单个字段,例如(名字、第二个名字或先生姓名)。对我来说,最大的问题是如何在同一个文本输入字段或任意数量的文本输入字段上搜索多个字段,例如(text1、text2、text3)我唯一想要的就是有确切的结果。对不起,如果有什么错误 这里是我用来获取单字段搜索的php代码php在msql上搜索多个字段并返回准确答案,php,mysql,Php,Mysql,我想我可能会改变问题的解释,因为没有任何帮助,但我真的需要帮助。 我的表单中有一个搜索框,允许用户在mysql表中搜索学生数据,我只成功搜索单个字段,例如(名字、第二个名字或先生姓名)。对我来说,最大的问题是如何在同一个文本输入字段或任意数量的文本输入字段上搜索多个字段,例如(text1、text2、text3)我唯一想要的就是有确切的结果。对不起,如果有什么错误 这里是我用来获取单字段搜索的php代码 <html> <head></head> <bod
<html>
<head></head>
<body><input type="text" name="query" value=""/>
<input name="submit" type="submit" value="Search" />
<?php
$query="query";
//mysql_connect
$query='query';
if (isset($_GET['query']))
{
$query=$_GET['query'];
// Instructions if $_POST['value'] exist
}
$raw_results = mysql_query("SELECT * FROM stdreg_exam
WHERE (`fname` LIKE '%".$query."%') or (`secname` LIKE '%".$query."%')or
(`date` LIKE '%".$query."%') or (`surname` LIKE '%".$query."%')") or
die(mysql_error());
$raw_results2 = mysql_query("SELECT(idnumber) FROM student
WHERE (`fname` LIKE '%".$query."%') or (`secname` LIKE
'%".$query."%') or (`date` LIKE '%".$query."%')or
(`surname` LIKE '%".$query."%')") or die(mysql_error());
// * means that it selects all fields, you can also write: `id`, `title`, `text`
// articles is the name of our table
// '%$query%' is what we're looking for, % means anything, for example if $query is Hello
// it will match "hello", "Hello man", "gogohello", if you want exact match use `title`='$query'
// or if you want to match just full word so "gogohello" is out use '% $query %' ...OR ... '$query %' ... OR ... '% $query'
if(mysql_num_rows($raw_results) > 0){
// if one or more rows are
returned do following
while($results = mysql_fetch_array($raw_results)){
while($results2 = mysql_fetch_array($raw_results2)){
// $results = mysql_fetch_array($raw_results) puts data from database into
array, while it's valid it does the loop
echo "<table width='750' height='5' cellpadding='2'
cellspacing='0' border='0'>";
echo"<tr><td>Std_id</td><td>Mathematics</td><td>English</td>
<td>Kiswahili</td><td>Geograph</td><td>Ict</td><td>Science</td>
<td>History</td><td>Pds</td><td>V skill</td><td>French</td>
<td>Religion</td><td>Civics</td>";
echo "<h4> ".$results['exam_name']." Examination result for
" .$results['fname']." " .$results['secname']
." ".$results['surname']." ".$results['class']." Class"."
held on</p>".$results['date']."<hr><th>"; echo"<tr>";
echo ""."<td>".$results2['idnumber'].""."<td>".$results['mathematics']."%"."
<td>".$results['english']."%"."<td>".$results['kiswahili']."%"."
<td>".$results['geograph']."%"."<td>".$results['ict']."%"."
<td>".$results['science']."%"."<td>".$results['history']."%"."
<td>".$results['pds']."%"."<td>".$results['vskill']."%"."
<td>".$results['french']."%"."<td>".$results['religion']."%"."
<td>".$results['civics']."%"."</td></p>";echo"</table>";
//posts results gotten from database(title and text) you can also show id
($results['id'])
}
}
}
else{
// if there is no matching rows do following
echo "No such information in School database";
}
}
else{
// if query length is less than minimum
echo "Enter more strings!!!Minimum length is ".$min_length; "Charactes";
}
?>
如果您有3个文本字段,比如text1、text2和text3,那么您需要从数据库中获取搜索结果。你可以试试这样的
$query = "SELECT your_feilds,another_fields FROM your_table WHERE 1=1 ";
if($_POST['text1'])
$query .= " AND text1_field like '%$_POST['text1']%' ";
if($_POST['text2'])
$query .= " AND text2_field like '%$_POST['text2']%' ";
if($_POST['text3'])
$query .= " AND text3_field like '%$_POST['text3']%' ";
$result = mysql_query($query);
来自#Shafeeq的上述答案非常实用,并且有效,因为我在我的一个应用程序中使用了相同的方法,但有一个问题,如果您有另一个字段,让我们说日期,您希望在两个日期之间搜索。
查询将是这样的
if($_POST['from_date'] && $_POST['To_date'])
$query .= " AND date between '$from_field' AND '$To_field' ";
那么它不适用于其他文件重复的$raw_results和$raw_results 2是否显示任何错误,或者它们是否正在检索所需的数据?是的!正在检索必需的数据OK,但当我说1=1时,这是否意味着它仅限于一行?