0

我正在编写一个 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 ...

4

2 回答 2

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 ...",
];
于 2012-07-21T23:05:48.793 回答
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'"
];
于 2012-07-21T23:06:22.953 回答