简体   繁体   English

从PHP中的准备好的语句打印整个SQL结果

[英]Printing an entire SQL result from a prepared statement in PHP

I'm trying to build a function that allows me to abstract the use of the mysqli functions in my code. 我正在尝试构建一个函数,该函数允许我抽象代码中mysqli函数的使用。 Currently I have a working version for using standard SQL (no prepared statements). 目前,我有一个使用标准SQL的工作版本(没有准备好的语句)。

function mySqlQueryToArray($con, $sql){
    // Process SQL query
    $result = mysqli_query($con, $sql);

    if(mysqli_error($con)){
        out($sql . "\n");
        exit("ERROR: MySQL Error: " . mysqli_error($con));
    }

    // Put result  into 2D array
    $output = array();
    while($row = mysqli_fetch_assoc($result)) {
        array_push($output, $row);
    }

    return $output;
}

Now I'm trying to translate that code into something that can use prepared statements but my research has revealed that when using prepared statements you need to bind each column to a different variable using mysqli_stmt::bind_result which causes a problem because the point of the function is to work for an arbitrary SQL Query. 现在,我试图将代码转换为可以使用预处理语句的代码,但是我的研究表明,使用预处理语句时,您需要使用mysqli_stmt::bind_result将每一列绑定到不同的变量,这会导致问题,因为功能是为任意SQL查询工作。

My main question is this: Is there a way to print out the entire output from a SQL query in a 2D array same as the function above does using prepared statements? 我的主要问题是:是否有一种方法可以将SQL查询的整个输出以2D数组的形式打印出来,就像上面的函数使用预处理语句一样?

Here's my current code for using prepared statements, it has everything but the bind_result in there. 这是我当前使用准备好的语句的代码,除了bind_result之外,所有内容都包含在内。

//Creates a MySQL Query, gets the result and then returns a 2D array with the results of the query
function mySqlQueryToArray($con, $sql, $params){
    // Prepare SQL query
    $stmt = $con->prepare($sql);

    if(mysqli_error($con)){
        echo "$sql=>\n" . print_r($params, true) . "\n";
        exit("$sql=>\n" . print_r($params, true) . "\nERROR: MySQL Error: " . mysqli_error($con));
    }

    // Bind parameters
    foreach($params as $param){
        $type = "s";
        if(gettype($param) == 'integer'){
            $type = "i";
        }
        $stmt->bind_param($type, $param);
    }

    //execute query
    $stmt->execute();

    // Put result  into 2D array
    $output = array();
    // ??? need to bind results and get them into an array somehow

    //close prepared statement
    $stmt->close();

    return $output;
}

PDO turns out to be the answer. 事实证明,PDO是答案。 I feel like I should post the code I created to solve my problem. 我觉得应该发布为解决问题而创建的代码。 Thanks to @Don'tPanic for the tip. 感谢@ Don'tPanic的提示。

function pdoQuery($sql, $params){
    $db = new PDO('mysql:host=localhost;dbname=My_Database;charset=utf8', 'username', 'password', array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
    try {
        $stmt = $db->prepare($sql);
        $stmt->execute($params);
        $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
        return $rows;
    }catch(PDOException $ex){
        echo "ERROR: SQL Error: $sql";
        // logError("ERROR: SQL Error: $sql");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM