我有这个查询我需要MYSQL查询忽略WHERE子句中的空变量并将发送变量发送到bind_param
这是代码
<?php
/**
* Created by PhpStorm.
* User: YousefAltaf
* Date: 11/1/2015
* Time: 11:40 AM
*/
//post vars
$institute = $_POST['institute'];
if (isset($_POST['sections'])) {
$sections = $_POST['sections'];
}
if (isset($_POST['division'])) {
$division = $_POST['division'];
}
if (isset($_POST['level'])) {
$level = $_POST['level'];
}
//check empty var
$where = "WHERE a.institute =?";
$bind = "i";
$prams = "$"."institute, ";
if (!empty($sections)) {
$where .= " AND a.section =?";
$bind .= "i";
$prams .= "$"."sections, ";
}
if (!empty($division)) {
$where .= " AND a.division =?";
$bind .= "i";
$prams .= "$"."division, ";
}
if (!empty($level)) {
$where .= " AND a.phase =?";
$bind .= "i";
$prams .= "$"."level";
}
//var_dump($institute, $sections, $division, $level);
var_dump($bind);
print_r ($prams);
if ($getSearch = $db->prepare("SELECT
a.*, a.id AS stud_id, b.id, b.ins_name, c.id, c.sec_name, d.id, d.div_name
FROM student_basic_info AS a
JOIN institutes AS b ON (a.institute = b.id)
CROSS JOIN ins_sections AS c ON (a.section = c.id)
CROSS JOIN ins_division AS d ON (a.division = d.id)
$where GROUP BY a.id
")
) {
$studSearch = array();
$getSearch->bind_param('iii', $prams);
} else {
printf("Errormessage: %s\n", $db->error);
}
if ($getSearch->execute()) {
$results = $getSearch->get_result();
while ($vStud = mysqli_fetch_array($results)) {
$studSearch[] = $vStud;
?>
得到了
警告:mysqli_stmt :: bind_param():类型中的元素数 定义字符串与绑定变量数不匹配