我正在编写一个PHP应用程序,其中一个功能是能够查询所有表(gene_r, genes_newL_dn, genes_newL_up, genes_oldL_up, genes_oldL_dn)
中的基因交互。
所以我有以下PHP
函数来一次查询所有这些数据库。
public function getAllInteractions($input){
$data = array();
$sql_list = (
"SELECT * FROM genes_r JOIN drugs_r ON drugs_r.id = genes_r.id WHERE drug_name='$input' OR gene_name='$input'",
"SELECT * FROM genes_newL_dn JOIN drugs_newL_dn ON drugs_newL_dn.id = genes_newL_dn WHERE drug_name='$input' OR gene_name='$input'",
"SELECT * FROM genes_newL_up JOIN drugs_newL_up ON drugs_newL_up.id = genes_newL_up WHERE drug_name='$input' OR gene_name='$input'",
"SELECT * FROM genes_oldL_dn JOIN drugs_oldL_dn ON drugs_oldL_dn.id = genes_oldL_dn WHERE drug_name='$input' OR gene_name='$input'",
"SELECT * FROM genes_oldL_up JOIN drugs_oldL_up ON drugs_oldL_up.id = genes_oldL_up WHERE drug_name='$input' OR gene_name='$input'"
);
foreach($sql_list as $sql){
$query = $this->db->query($sql);
// case 1 : SQL Query invalid / empty results
if(!$query || $query->num_rows() == 0) {
continue;
}
else {
$id = $query->row()->id;
$sql = "SELECT interaction from matrix_r WHERE id='$id'";
$query = $this->db->query($sql);
array_push($data, $query->row());
}
}
return $data;
}
但是,我收到以下错误:
PHP Parse error: syntax error, unexpected ',' in
/chemicalgenomics/ci/application/models/search_model.php on line 81
我不明白为什么不喜欢','。它应该是一个字符串数组。有人可以帮帮我吗? 感谢
PS:Line 81
是数组中的第一行:"SELECT * FROM ...
答案 0 :(得分:5)
您初始化数组错误,您需要将array
放在开头:
$sql_list = array(
"SELECT * FROM genes_r JOIN drugs_r ON drugs_r.id = genes_r.id WHERE drug_name='$input' OR gene_name='$input'",
"SELECT * FROM genes_newL_dn JOIN drugs_newL_dn ON drugs_newL_dn.id = genes_newL_dn WHERE drug_name='$input' OR gene_name='$input'",
"SELECT * FROM genes_newL_up JOIN drugs_newL_up ON drugs_newL_up.id = genes_newL_up WHERE drug_name='$input' OR gene_name='$input'",
"SELECT * FROM genes_oldL_dn JOIN drugs_oldL_dn ON drugs_oldL_dn.id = genes_oldL_dn WHERE drug_name='$input' OR gene_name='$input'",
"SELECT * FROM genes_oldL_up JOIN drugs_oldL_up ON drugs_oldL_up.id = genes_oldL_up WHERE drug_name='$input' OR gene_name='$input'"
);
如果您使用的是PHP 5.4或更高版本(但并不是那么多的Web主机),您可以使用您接近的新的短数组语法,但该字符是括号([
)而不是括号((
):
$sql_list = [
"SELECT ...",
"SELECT ...",
];
答案 1 :(得分:1)
$sql_list = array(x, y, z);
是PHP数组的正确语法。因此,在您的代码中,它应该是:
$sql_list = array(
"SELECT * FROM genes_r JOIN drugs_r ON drugs_r.id = genes_r.id WHERE drug_name='$input' OR gene_name='$input'",
"SELECT * FROM genes_newL_dn JOIN drugs_newL_dn ON drugs_newL_dn.id = genes_newL_dn WHERE drug_name='$input' OR gene_name='$input'",
"SELECT * FROM genes_newL_up JOIN drugs_newL_up ON drugs_newL_up.id = genes_newL_up WHERE drug_name='$input' OR gene_name='$input'",
"SELECT * FROM genes_oldL_dn JOIN drugs_oldL_dn ON drugs_oldL_dn.id = genes_oldL_dn WHERE drug_name='$input' OR gene_name='$input'",
"SELECT * FROM genes_oldL_up JOIN drugs_oldL_up ON drugs_oldL_up.id = genes_oldL_up WHERE drug_name='$input' OR gene_name='$input'"
);
如果您使用的是PHP 5.4,则可以使用缩短的语法:
$sql_list = [
"SELECT * FROM genes_r JOIN drugs_r ON drugs_r.id = genes_r.id WHERE drug_name='$input' OR gene_name='$input'",
"SELECT * FROM genes_newL_dn JOIN drugs_newL_dn ON drugs_newL_dn.id = genes_newL_dn WHERE drug_name='$input' OR gene_name='$input'",
"SELECT * FROM genes_newL_up JOIN drugs_newL_up ON drugs_newL_up.id = genes_newL_up WHERE drug_name='$input' OR gene_name='$input'",
"SELECT * FROM genes_oldL_dn JOIN drugs_oldL_dn ON drugs_oldL_dn.id = genes_oldL_dn WHERE drug_name='$input' OR gene_name='$input'",
"SELECT * FROM genes_oldL_up JOIN drugs_oldL_up ON drugs_oldL_up.id = genes_oldL_up WHERE drug_name='$input' OR gene_name='$input'"
];