我的功能看起来像那样。
if (isset($_POST['type'])) {
switch ($_POST['type']) {
case "qsubject":
$sql = "SELECT id, name FROM chapters WHERE subject_id=?";
break;
case "qchapters":
$sql = "SELECT id, name FROM sections WHERE subject_id=? AND chapter_id=?";
break;
case "qsections":
$sql = "SELECT id, name FROM paragraphs WHERE subject_id=? AND chapter_id=? AND section_id=?";
break;
}
$stmt = $db->prepare($sql) or die($db->error());
$stmt->bind_param("i", $id);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows > 0) {
$stmt->bind_result($chp_id, $name);
echo '<option value="#"></option>';
while ($stmt->fetch())
echo '<option value="' . $chp_id . '">' . $name . '</option>';
}
else
echo 0;
$stmt->close();
}
对于每个切换案例$params
($stmt->bind_param($params);
)必须不同。
所以我想做的是在$params
switch
$params='"i"'.$id;
然后像这样使用它
$stmt->bind_param($params);
这可能吗?
答案 0 :(得分:1)
我真的不知道这是不是你想要的,只是把prepare()和bind_param()调用放在case语句中:
if (isset($_POST['type'])) {
switch ($_POST['type']) {
case "qsubject":
$sql = "SELECT id, name FROM chapters WHERE subject_id=?";
$stmt = $db->prepare($sql) or die($db->error());
$stmt->bind_param("i", $id);
break;
case "qchapters":
$sql = "SELECT id, name FROM sections WHERE subject_id=? AND chapter_id=?";
$stmt = $db->prepare($sql) or die($db->error());
$stmt->bind_param("ii", $id, $chapter_id);
break;
case "qsections":
$sql = "SELECT id, name FROM paragraphs WHERE subject_id=? AND chapter_id=? AND section_id=?";
$stmt = $db->prepare($sql) or die($db->error());
$stmt->bind_param("iii", $id, $chapter_id, $section_id);
break;
}
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows > 0) {
$stmt->bind_result($chp_id, $name);
echo '<option value="#"></option>';
while ($stmt->fetch())
echo '<option value="' . $chp_id . '">' . $name . '</option>';
}
else
echo 0;
$stmt->close();
}
你正在做3个不同的查询,所以有3个不同的prepare() - 调用完全没问题。
如果你不能使用这种方法,你也可以创建一个简单的数组来存储你的参数。
$params = array();
case "qsubject":
$params[] = array("type"=>"i", "value"=>$id);
break;
case "qchapters":
$params[] = array("type"=>"i", "value"=>$id);
$params[] = array("type"=>"i", "value"=>$chapter_id);
break;
// probably won't work, see text below.
foreach ($params as $param)
$stmt->bind_param($param["type"], $param["value"]);
在调用bind_param语句时,似乎必须提供所有变量。你仍然可以使用可变的参数计数来调用bind_param。请参阅http://php.net/manual/de/mysqli-stmt.bind-param.php上有关解决方案的评论。
答案 1 :(得分:0)
您可以在每个case语句中绑定params,如下所示:
switch ($_POST['type']) {
case "qsubject":
$sql = "SELECT id, name FROM chapters WHERE subject_id=?";
$stmt = $db->prepare($sql) or die($db->error());
$stmt->bind_param("i", $id);
break;
case "qchapters":
$sql = "SELECT id, name FROM sections WHERE subject_id=? AND chapter_id=?";
$stmt = $db->prepare($sql) or die($db->error());
$stmt->bind_param("ii", $id, $id1);
break;
case "qsections":
$sql = "SELECT id, name FROM paragraphs WHERE subject_id=? AND chapter_id=? AND section_id=?";
$stmt = $db->prepare($sql) or die($db->error());
$stmt->bind_param("iii", $id, $id1, $id2);
break;
}
答案 2 :(得分:0)
也许它不再对OP有所帮助,但我猜他真正想要的是动态创建一个数组(具有可变长度的参数)来绑定。
这可以通过
来完成call_user_func_array()
简短的例子:
call_user_func_array(array($stmt, 'bind_param'),
array_merge(array($bindParamsString), $paramsToBind));
其中变量$bindParamsString
对应于参数类型(例如'iis'),参数$paramsToBind
是一个数组,其中包含对要绑定的值的引用:
$paramsToBind = array(&myFirstValue [,&mySecondValue [,..]);
答案 3 :(得分:-2)
是的,这很有可能,但我建议不要使用mysqli并转移到PDO,它的工作方式完全符合您的要求:
$stmt = $db->prepare($sql);
$stmt->execute($params);
$data = $stmt->fetchAll();