在现有函数中使用mysqli_multi_query

时间:2019-06-16 17:39:13

标签: php mysqli mysqli-multi-query

我有一个可以建立不同类型的数据库连接的函数,下面仅用两个$ ActionType值进行了简化,这是我遇到的第二个困难。没有错误,也没有结果。有什么想法吗?

查询包含多个查询,例如:

$Query = "SQL statement #1;";
$Query .= "SQL statement #2;";
$Query .= "SQL statement #3;";

这是函数,请缩小为适合这里:

function DBConnect($Query, $ActionType, $DBname, $selType='array') {
    $ActionType = trim(strtolower($ActionType));

    if (!$Query) :
        exit();
    endif;

    $MySQLError = "";
    $mysqli = dbConn($DBname);

    if ($Query && $mysqli->connect_errno != 0):
        $MySQLError = "<div class=\"ErrorMessage\">";
        $MySQLError .= printf("Connect failed: %s\n", $mysqli->connect_error);
        $MySQLError .= "</div>\n\n";
        return $MySQLError;
        exit();
    endif;

    switch ($ActionType) :
        case "multiple":
            if ($result = $mysqli->query($Query)) :
                $numrowsCat = $result->num_rows;
                if ($numrowsCat >= 1) :
                    $result = $mysqli->query($Query);
                    if ($selType === "assoc") :
                        while($row = $result->fetch_assoc()) :
                            $results_array[] = $row;
                        endwhile;
                    else :                  
                        while($row = $result->fetch_array()) :
                            $results_array[] = $row;
                        endwhile;
                    endif;
                    return $results_array;
                endif;
                $MySQLError = ($mysqli->connect_errno) ? mysqli_error($mysqli) : "";
                $mysqli->close();
                if ($MySQLError) return $MySQLError;
            endif;
        break;

        case "multiquery":
            if ($result = $mysqli->multi_query($Query)) :
                if ($result) :
                    while($row = $result->fetch_array()) :
                        $results_array[] = $row;
                    endwhile;
                    return $results_array;
                endif;

                $MySQLError = ($mysqli->connect_errno) ? mysqli_error($mysqli) : "";
                $mysqli->close();
                if ($MySQLError) return $MySQLError;
            endif;
        break;

    endswitch;
}

0 个答案:

没有答案