如何将参数数组绑定到MySQLi语句?

时间:2015-04-05 01:55:11

标签: php mysqli

我在php中创建一个使用MySQL数据库的网站。我使用MySQLi编程数据库函数调用。我想编写一个通用函数来处理mysql语句的编写。这是非工作代码:

<?php

function execute_statement($mysql, $statement, $typeDef = FALSE, $params = FALSE){
    if(($stmt = $mysql->prepare($statement))){

        if($typeDef){
            // bind params
            $stmt->bind_param($typeDef, $params);
        }

        // store result
        $stmt->store_result();

        // execute the statement
        $stmt->execute();

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

        // return result
        return true;
    }
    return false;
}

function add_event($mysql, $eventTitle, $eventDescription)
{
    // execute statement
    if(!execute_statement($mysql, "INSERT INTO events "
            . "(title, "
            . "description) "
            . "VALUES(?, ?)",
            "ss",
            array(&$eventTitle, &$eventDescription))){
        $error = "Database error";
        $details = "Failed to add an event to the database";
        return json_encode(
                array(
                    "result"=>false, 
                    "error"=>$error, 
                    "details"=>$details,
                    "params"=>array(
                        "title"=> $title,
                        "description"=>$desc)));
    }
}

?>

execute_statement方法用于&#39;插入&#39;只有陈述。如何将我的参数数组绑定到语句?

0 个答案:

没有答案