php 条件搜索mysql_具有多个条件的搜索功能 - PHP / MySQL (Search feature with multiple criteria - PHP/MySQL)...

英文原文

You can check to see if the post for a particular field is empty, if it's not then append the corresponding WHERE clause to the query. Something in the form of the following:

$mysqli = new mysqli(...);

$sql = 'SELECT * FROM table WHERE ';

$where = array();

$values = array();

$types = '';

if (!empty($_POST['name'])) {

$where[] = 'name = ?';

$values[] = $_POST['name'];

$types .= 's';

}

if (!empty($_POST['sex'])) {

$where[] = 'sex = ?';

$values[] = $_POST['sex'];

$types .= 's';

}

...

$sql .= implode(' AND ',$where);

$values = array_unshift($values, $types);

$statement = $mysqli->prepare($sql);

call_user_func_array(array($statement, 'bind_param'), $values);

...

中文翻译

您可以检查特定字段的帖子是否为空,如果不是,则将相应的WHERE子句附加到查询中。以下形式的东西:

$ mysqli = new mysqli(...);

$ sql ='SELECT * FROM table WHERE';

$ where = array();

$ values = array();

$ types ='';

if(!empty($ _ POST ['name'])){

$ where [] ='name =?';

$ values [] = $ _POST ['name'];

$ types。='s';

}

if(!empty($ _ POST ['sex'])){

$ where [] ='sex =?';

$ values [] = $ _POST ['sex'];

$ types。='s';

}

...

$ sql。= implode('AND',$ where);

$ values = array_unshift($ values,$ types);

$ statement = $ mysqli-> prepare($ sql);

call_user_func_array(array($ statement,'bind_param'),$ values);

...

You can check to see if the post for a particular field is empty, if it's not then append the corresponding WHERE clause to the query. Something in the form of the following:

$mysqli = new mysqli(...);

$sql = 'SELECT * FROM table WHERE ';

$where = array();

$values = array();

$types = '';

if (!empty($_POST['name'])) {

$where[] = 'name = ?';

$values[] = $_POST['name'];

$types .= 's';

}

if (!empty($_POST['sex'])) {

$where[] = 'sex = ?';

$values[] = $_POST['sex'];

$types .= 's';

}

...

$sql .= implode(' AND ',$where);

$values = array_unshift($values, $types);

$statement = $mysqli->prepare($sql);

call_user_func_array(array($statement, 'bind_param'), $values);

...

您可以检查特定字段的帖子是否为空,如果不是,则将相应的WHERE子句附加到查询中。以下形式的东西:

$ mysqli = new mysqli(...);

$ sql ='SELECT * FROM table WHERE';

$ where = array();

$ values = array();

$ types ='';

if(!empty($ _ POST ['name'])){

$ where [] ='name =?';

$ values [] = $ _POST ['name'];

$ types。='s';

}

if(!empty($ _ POST ['sex'])){

$ where [] ='sex =?';

$ values [] = $ _POST ['sex'];

$ types。='s';

}

...

$ sql。= implode('AND',$ where);

$ values = array_unshift($ values,$ types);

$ statement = $ mysqli-> prepare($ sql);

call_user_func_array(array($ statement,'bind_param'),$ values);

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值