如何在执行多个查询时返回多个结果?

时间:2017-12-30 13:22:44

标签: php mysql sql

我在函数内执行4个查询。我如何一次返回这4个结果,以便在调用函数时可以使用所有4个结果?我应该创建一个数组并将所有结果存储在该数组中吗?我试图逐个返回结果,但在调用return命令后,函数退出该点。有没有办法单独返回每个查询结果?这是我的代码:

function select_all_category_name() {
        require 'admin/db_connect.php'; 
        $sql="SELECT * FROM tbl_category WHERE publication_status=1 AND deletion_status=1";

        if (mysqli_query($db_connect, $sql)) {

            $sql1="SELECT * FROM tbl_category WHERE publication_status=1 AND deletion_status=1 AND mpid='' AND spid='' AND pid=''";
            $sql2="SELECT * FROM tbl_category WHERE publication_status=1 AND deletion_status=1 AND mpid!='' AND spid='' AND pid=''";
            $sql3="SELECT * FROM tbl_category WHERE publication_status=1 AND deletion_status=1 AND mpid!='' AND spid!='' AND pid=''";
            $sql4="SELECT * FROM tbl_category WHERE publication_status=1 AND deletion_status=1 AND mpid!='' AND spid!='' AND pid!=''";
            $query_result1=mysqli_query($db_connect, $sql1);
            $query_result2=mysqli_query($db_connect, $sql2);
            $query_result3=mysqli_query($db_connect, $sql3);
            $query_result4=mysqli_query($db_connect, $sql4);


        } else {
            die("Query Problem". mysqli_error($db_connect));
        }
    }

db_connect.php文件包含连接。

3 个答案:

答案 0 :(得分:1)

使用数组:

function select_all_category_name() {
    require 'admin/db_connect.php'; 
    $sql="SELECT * FROM tbl_category WHERE publication_status=1 AND deletion_status=1";

    if (mysqli_query($db_connect, $sql)) {
        $ret = [
             "{$sql} AND mpid ='' AND spid ='' AND pid =''",
             "{$sql} AND mpid!='' AND spid ='' AND pid =''",
             "{$sql} AND mpid!='' AND spid!='' AND pid =''",
             "{$sql} AND mpid!='' AND spid!='' AND pid!=''",
        ];
        foreach ($ret as $i => $sqlx) {
            $ret[$i] = mysqli_query($db_connect, $sqlx);
        };
        return $ret;
    }
    return false;
}

或者您可以对整个函数使用单个查询(如果找不到任何内容,它将返回一个空数组):

SELECT
    CASE 
        WHEN mpid='' AND spid='' AND pid='' THEN 0,
        WHEN mpid!='' AND spid='' AND pid='' THEN 1,
        WHEN mpid!='' AND spid!='' AND pid='' THEN 2,
        WHEN mpid!='' AND spid!='' AND pid!='' THEN 3
        ELSE -1
    END AS idx,
    *
FROM tbl_category 
WHERE publication_status=1 AND deletion_status=1
HAVING idx != -1

答案 1 :(得分:0)

功能只能返回'一个价值。因此,您无法单独返回所有4个查询结果。数组是您唯一的选择。

答案 2 :(得分:0)

也许是这样的?

function select_all_category_name() {
    require 'admin/db_connect.php'; 
    $sql="SELECT * FROM tbl_category WHERE publication_status=1 AND deletion_status=1";

    if (mysqli_query($db_connect, $sql)) {

        $sql1="SELECT * FROM tbl_category WHERE publication_status=1 AND deletion_status=1 AND mpid='' AND spid='' AND pid=''";
        $sql2="SELECT * FROM tbl_category WHERE publication_status=1 AND deletion_status=1 AND mpid!='' AND spid='' AND pid=''";
        $sql3="SELECT * FROM tbl_category WHERE publication_status=1 AND deletion_status=1 AND mpid!='' AND spid!='' AND pid=''";
        $sql4="SELECT * FROM tbl_category WHERE publication_status=1 AND deletion_status=1 AND mpid!='' AND spid!='' AND pid!=''";

        $result_array = array(
            "query_result1" => mysqli_query($db_connect, $sql1),
            "query_result2" => mysqli_query($db_connect, $sql2),
            "query_result3" => mysqli_query($db_connect, $sql3),
            "query_result4" => mysqli_query($db_connect, $sql4)
        );

     return $result_array;
    } else {
        die("Query Problem". mysqli_error($db_connect));
    }
}