英文原文
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);
...