仅准备一次语句的动态准备语句现成函数

问题描述 投票:-1回答:1

我想要一个仅准备一次语句的函数。 (并非每次函数都调用相同的Query时)。因为Prepared语句的优点之一是:对查询的准备仅执行一次(尽管该语句执行了多次)

因此,每当我调用此函数时,它都会每次都准备语句,这是不合适的,因为我们会错过利用准备语句最大的优势之一。因此,我将条件if( !empty($stmtName) and !isset($GLOBALS[$stmtName]) )设置为Check Statement已设置或未设置。 (如果不是这样,那么只有语句才能准备好)使用此方法,我们将覆盖该优势。 但是,生成绑定参数失败错误。

我的功能在这里...

function qryInsert( $stmtName, $table, $field, $params, $formats )
    {

        $query = " INSERT INTO ".$table
                ." ".( (isset($field) and !empty($field)) ? " ( ".(implode(", ",$field))." ) " : " " ). " "
                ." VALUES( ". implode(", ", array_map(function($val) { return "?"; }, $field))." ) ";

        /*if(!isset($con) or empty($con))
        {
            $con = $this->connection();
        }*/


        $a_params = array();


        $a_params = array();

        $param_type = '';
        $n = count($formats);

        for($i = 0; $i < $n; $i++)
        {
            $param_type .= $formats[$i];
        }

        $a_params[] = & $param_type;

        for($i = 0; $i < $n; $i++)
        {
            $a_params[] = & $params[$i];
        }

        if( !empty($stmtName) and !isset($GLOBALS[$stmtName]) )
        {
            $GLOBALS[$stmtName] = $GLOBALS['con']->prepare($query);
            // $stmt = $con->prepare($query);
        }

        if(!($GLOBALS[$stmtName]))
        {
            echo " Prepare failed: (" . $con->errno . ") " . $con->error; // . " <br> Query : <span style='color:tomato;'> ".$query." </span>"
        }
        else
        {

            if(!(call_user_func_array(array($GLOBALS[$stmtName], 'bind_param'), $a_params)))
            {
                echo "Binding parameters failed: (" . $GLOBALS[$stmtName]->errno . ") " . $GLOBALS[$stmtName]->error;
            }
            else
            {
                if(!($GLOBALS[$stmtName]->execute()))
                {
                    echo "Execute failed: (" . $GLOBALS[$stmtName]->errno . ") " . $GLOBALS[$stmtName]->error;
                }
                else
                {
                    if($meta = $GLOBALS[$stmtName]->result_metadata())
                    {
                        while ($field = $meta->fetch_field())
                        {
                            $columns[] = &$row[$field->name];
                        }

                        if(call_user_func_array(array($GLOBALS[$stmtName], 'bind_result'), $columns))
                        {
                            while ($GLOBALS[$stmtName]->fetch())
                            {
                                foreach($row as $key => $val)
                                {
                                    $x[$key] = $val;
                                }
                                $results[] = $x;
                            }
                        }
                        else
                        {
                            echo " Error occur while Bindig Result...";
                        }
                    }
                }
            }
        }

        $GLOBALS[$stmtName]->close();

        return $results;
    }

INPUT:

qryInsert("insStud", "student_master", array("roll_no","name"), array(21,"Mahetab"), array("i","s"));
qryInsert("insStud", "student_master", array("roll_no","name"), array(8,"Sahil"), array("i","s"));
qryInsert("insStud", "student_master", array("roll_no","name"), array(10,"Mahammad"), array("i","s"));

输出:

First time Record will Insert...
After that raised Binding Parameter failed error...

无此条件 if( !empty($stmtName) and !isset($GLOBALS[$stmtName]) )

我的代码运行正常...没有任何问题...因为它将每次都准备语句

我使用$ GLOBALS变量,以便每当调用函数时,它都使用相同的GLOBALS变量,否则,函数将使用无法正常工作的私有变量执行操作

php mysql function dynamic prepared-statement
1个回答
0
投票

问题已解决...只想删除$GLOBALS[$stmtName]->close();语句...因为每次函数调用时它都会关闭语句...因此,绑定参数失败...

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.