我有很长的多个SQL查询,需要多次使用不同的ID。我认为将这些查询放在方法中的数组中会更好,这样我就可以随时调用它。
问题是,我试图将ID变量放入查询中时遇到困难。简单来说,代码是这样的:
class SqlQueries {
protected $_queries;
private function $_queriesArr() {
$_queries = array(
"SELECT * FROM tbl_a WHERE id = $id_a",
"UPDATE tbl_b SET b = NULL WHERE id = $id_b",
"UPDATE tbl_c SET c = NULL WHERE id = $id_c",
);
return $_queries;
}
public function $callQuery() {
$id_a = 2;
$tbl_a = $this->$_queriesArr();
return $tbl_a[0];
}
public function $mainMethod() {
echo $this->callQuery();
}
}
我总是在查询中遇到错误。
答案 0 :(得分:0)
如何添加这样的参数,但为什么命名函数以$开头? 顺便说一下,只需在您的函数中添加参数,并希望它能够正常运行
private function queriesArr($id_a, $id_b, $id_c) {
$_queries = array(
"SELECT * FROM tbl_a WHERE id = $id_a",
"UPDATE tbl_b SET b = NULL WHERE id = $id_b",
"UPDATE tbl_c SET c = NULL WHERE id = $id_c",
);
return $_queries;
}
答案 1 :(得分:0)
class SqlQueries {
protected $_queries;
private function $_queriesArr($id_a='', $id_b='', $id_c='') {
$_queries = array(
"SELECT * FROM tbl_a WHERE id = '".$id_a."' ",
"UPDATE tbl_b SET b = NULL WHERE id = '".$id_b."' ",
"UPDATE tbl_c SET c = NULL WHERE id = '".$id_c."' ",
);
return $_queries;
}
public function $callQuery() {
$id_a = 2;
$tbl_a = $this->$_queriesArr($id_a);
return $tbl_a[0];
}
public function $mainMethod() {
echo $this->callQuery();
}
}