我想从我的HTML表单中查询3个表。我尝试了很多格式和条件,但这是唯一一个接近我想要实现的格式和条件。我是新手,我需要帮助。我不知道我错过了什么以显示输出。
这是我的代码:
if(isset($_POST['submit'])) {
$Summary=$_POST['summary'];
$Status=$_POST['status'];
$Priority=$_POST['priority'];
$db=mysql_connect (" ") or die ('I cannot connect to the database because: ' . mysql_error());
$mydb=mysql_select_db(" ");
}
$where = '';
if(isset($Status) && !empty($Status)){
$where .= "status ='$Status' AND ";
}
if(isset($Priority) && !empty($Priority)){
$where .= "priority ='$Priority' AND ";
}
if(isset($Summary) && !empty($Summary)){
$where .= "summary ='$Summary' AND ";
}
$sql= "select * from data $where";
$result = mysql_query($sql);
if (mysql_num_rows($result)==0){
echo "No Match Found";
}else{
while($row = mysql_fetch_array($result)) {
echo $Not_Completed=$row['Not Completed'];
echo $Completed=$row['Completed'];
echo $Miscommunications =$row['Miscommunications'];
echo $Not_Given_Access=$row['Not Given Access'];
echo $Unskilled=$row['Unskilled'];
echo $Missing_Parts=$row['Missing Parts'];
echo $High=$row['High'];
echo $Medium=$row['Medium'];
echo $Low=$row['Low'];
echo $EFirst_name =$row['EFirst_name'];
echo $ELast_name=$row['ELast_name'];
echo $Employee_id=$row['Employee_id'];
}
}
这是我的HTML表单:
<select name="status">
<option>status</option>
<option>Completed</option>
<option>Not Completed</option>
</select>
<select name="priority">
<option>priority</option>
<option>High</option>
<option>Medium</option>
<option>Low</option>
</select>
<select name="summary">
<option>summary</option>
<option>Miscommunications</option>
<option>Not Given Access</option>
<option>Unskilled</option>
<option>Missing Parts</option>
</select>
<div align="right"><input type="submit" value="Search">
我收到警告:警告:mysql_num_rows()期望参数1是资源,布线在第191行的C:\ wamp \ www \ search.php中给出// // if(mysql_num_rows($ result)== 0 )” ...
欢迎所有建议。感谢。
答案 0 :(得分:0)
$sql = "select * from data where $where";
另外,这里:
if(isset($Status) && !empty($Status)){
$where .= "status ='$Status' AND ";
}
if(isset($Priority) && !empty($Priority)){
$where .= "priority ='$Priority' AND ";
}
if(isset($Summary) && !empty($Summary)){
$where .= "summary ='$Summary' AND ";
}
如果超过1 if
为true
,则会有一个尾随AND
在if
s:
$where .= '1=1';
答案 1 :(得分:0)
当mysql_num_rows()报告此错误时(99%)您的查询错误。
$sql= "select * from data $where";
似乎缺少where
个关键字。如果$where
不为空,请添加。
像:
$sql= "select * from data ".(empty($where)?"":"where ".substr($where, 0, -5)); // substr, for trimming the "AND"
答案 2 :(得分:0)
您的查询格式错误并导致错误。在查询字符串中指定$where
时,您忘记包含WHERE
语句。您需要执行:$sql= "select * from data where ".$where;
答案 3 :(得分:0)
尝试更改此部分代码
if(isset($Status) && !empty($Status)){
$where .= "status ='$Status' AND ";
}
if(isset($Priority) && !empty($Priority)){
$where .= "priority ='$Priority' AND ";
}
if(isset($Summary) && !empty($Summary)){
$where .= "summary ='$Summary' AND ";
}
到此代码
$check = 0;
if(isset($Status) && !empty($Status)){
$where .= "status ='$Status'";
$check++;
}
if(isset($Priority) && !empty($Priority)){
if($check != 0){
$where .= " AND ";
}
$where .= "priority ='$Priority'";
$check++;
}
if(isset($Summary) && !empty($Summary)){
if($check != 0){
$where .= " AND ";
}
$where .= "summary ='$Summary'";
}
答案 4 :(得分:0)
$conditions = array();
if(isset($Status) && !empty($Status)){
$conditions[] = "status ='$Status'";
}
if(isset($Priority) && !empty($Priority)){
$conditions[] = "priority ='$Priority'";
}
if(isset($Summary) && !empty($Summary)){
$conditions[] = "summary ='$Summary'";
}
$where = '';
if (!empty($conditions)) {
$where = "WHERE " . implode(' AND ', $conditions);
}
$sql= "SELECT * FROM data $where";
修改:您能否发布一下结果:
if(isset($_POST['submit'])) {
$Summary=$_POST['summary'];
$Status=$_POST['status'];
$Priority=$_POST['priority'];
$db=mysql_connect (" ") or die ('I cannot connect to the database because: ' . mysql_error());
$mydb=mysql_select_db(" ");
} else {
echo "I might not be connected to a database...";
}
echo 'Connection error? ' . mysql_error();
echo 'Ping Result: ' . mysql_ping() === FALSE ? 'Not connected.' : 'Connected.';
$conditions = array();
if(isset($Status) && !empty($Status)){
$conditions[] = "status ='$Status'";
}
if(isset($Priority) && !empty($Priority)){
$conditions[] = "priority ='$Priority'";
}
if(isset($Summary) && !empty($Summary)){
$conditions[] = "summary ='$Summary'";
}
$where = '';
if (!empty($conditions)) {
$where = "WHERE " . implode(' AND ', $conditions);
}
$sql= "SELECT * FROM data $where";
echo "SQL Command: $sql";
$result = mysql_query($sql);
echo 'Query error? ' . mysql_error();
if (mysql_num_rows($result)==0){
echo "No Match Found";
}else{
while($row = mysql_fetch_array($result)) {
echo $Not_Completed=$row['Not Completed'];
echo $Completed=$row['Completed'];
echo $Miscommunications =$row['Miscommunications'];
echo $Not_Given_Access=$row['Not Given Access'];
echo $Unskilled=$row['Unskilled'];
echo $Missing_Parts=$row['Missing Parts'];
echo $High=$row['High'];
echo $Medium=$row['Medium'];
echo $Low=$row['Low'];
echo $EFirst_name =$row['EFirst_name'];
echo $ELast_name=$row['ELast_name'];
echo $Employee_id=$row['Employee_id'];
}
}