sql和PHP中的多个部分搜索语句


Multiple partial search statements in sql and PHP

我目前正在使用以下输入搜索创建一个简单的人员mySQL搜索:

status(这是一个具有固定值1,2,3,4的选项列表)名字姓电子邮件电话描述

这是我当前的代码:

if($stmt = $db_connect->prepare("SELECT * FROM candidate WHERE status = ?
OR firstname = ?
OR surname = ?
OR email = ?
OR telephone_mobile = ?
OR description = ?"))
{
   $stmt->bind_param('isssis',$post_status, $post_firstname, $post_surname, $post_email, $post_phone, $post_desc);
   $stmt->execute();
   $main = $stmt->num_rows;
   $result = $stmt->get_result();
}
<table>
<?php 
while($row = $result->fetch_object()) {
?>
<tr>
<td><?php echo $row->firstname; ?></td>
<td><?php echo $row->surname; ?></td>
<td><?php echo $row->email; ?></td>
<td><?php echo $row->telephone_mobile; ?></td>
<td><?php echo $row->description; ?></td>
</tr>
<?php } $stmt->free_result(); ?>
</table>

我知道OR语句不正确,我一直在阅读AGAINSTLIKE语句,我不确定正确的方法,因为有时一些输入表格可能是空白的,而其他表格可能是完全填写的。

任何帮助都会很棒!:)

编辑:

Example Data
firstname | surname | email           | phone         | description
--------------------------------------------------------------------------------
john      | smith   | john@me.com     | 07645362839   | he has a SIA licence
larry     | howden  | lh@google.com   | 01574830487   | he is a beautiful man
jane      | jones   |                 | 01937437384   | has no email address

试试这个:

if(!empty($postrongtatus)){$binds[]=&$postrongtatus;$bind_type.="i";$criteria[]="状态=?";}if(!empty($post_firstname)){$post_firstname_val="%"$post_firstname。"%";$binds[]=&$post_firstname_val;$bind_type.="s";$criterial[]="firstname LIKE?";}if(!empty($postrongurname)){$postrongurname_val="%"$postrongurname。"%";$binds[]=&$postrongurname_val;$bind_type.="s";$criterial[]='姓LIKE?';}if(!empty($post_email)){$post_email_val="%"$post_email。"%";$binds[]=&$post_email_val;$bind_type.="s";$criterial[]='电子邮件LIKE?';}if(!empty($post_phone)){$post_phone_val="%"$post_phone。"%";$binds[]=&$post_phone_val;$bind_type.="s";$criterial[]="电话喜欢吗?";}if(!empty($post_desc)){$post_desc_val="%"$post_desc。"%";$binds[]=&$post_desc_val;$bind_type.="s";$criterial[]="description LIKE?";}如果(count($criteria)>0){$query.='WHERE’.内爆("OR",$criteria);}$stmt=$db_connect->prepare($query);如果(count($binds)>0){$params=array_merge(数组($bind_type),$binds);call_user_func_array(数组($stmt,'bind_param'),$params);}$stmt->execute();

对于每个非空搜索条件,它将添加一个条件以部分匹配该条件(如果输入整个值,则添加一个完整条件)。这将返回与添加到搜索中的任何条件匹配的所有行。

假设您有$post_firstname = "Jane"$post_pone = "0764536"

代码将返回第一行(因为它将部分匹配手机)和第四行,因为它将完全匹配姓名。

我认为很多评论都很有帮助,但实际上并不需要。

如果你想使用LIKE,你可以简单地更改你的查询如下:

SELECT * FROM candidate WHERE status LIKE 'status' OR firstname LIKE 'firstname'...

你可以用LIKE代替等号。如果你想包括元字符,你可以用一个%