stmt get_result 另一种方式

stmt get_result another way

我的一个查询示例...

public function db_query_select($query, $params, $param_types){
        $dbc = $this->dbConnect();
        if($stmt = $dbc->prepare($query)){
            //prepared.
            //move the types to the front of the param array
            array_unshift($params, $param_types);
            //call the bind param function with the parameters passed in by reference
            //bind_param only allows by reference.
          call_user_func_array(array($stmt, "bind_param"), $this->paramsToRefs($params));
                //binded.
                //attempt to execute the sql statement.
                if ($stmt->execute()){
                            $result = $stmt->get_result();
                            $stmt->close();
                            $dbc->close();
                            return $result;
                }
            }
            //must have failed...
            return NULL;
    }

如何更改 stmt get_result();共享 servers/hosts 无需本机驱动程序即可接受的内容... mysqlnd。

有人知道吗?无需更改我使用此数据库函数的所有函数。

谢谢。

已更新:::::感谢@您的常识,请参阅答案。

我相信这就是我想要的。希望它能帮助任何和我有同样问题的人。 PDO vs MySQLi,似乎更简单......没有用户调用 func 或类似的东西。

数据库处理程序:

private function dbConnect(){
        $config = parse_ini_file($_SERVER['DOCUMENT_ROOT'].'/NTConfig.ini');
        try {
        $dbc = new PDO('mysql:host='.$config['DB_HOST'].';dbname='.$config['DB_DATABASE'].'', $config['DB_USER'], $config['DB_PASSWORD']);
        $dbc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        } catch (PDOException $e) {
        echo 'Connection failed: ' . $e->getMessage();
        exit;
    }
        return $dbc;

    }


public function db_query_select($query, $params){
        $dbc = $this->dbConnect();
        if($stmt = $dbc->prepare($query)){
            //prepared.
                //attempt to execute the sql statement.
                if ($stmt->execute($params)){
                    $result = $stmt->fetch(PDO::FETCH_ASSOC);
                    print_r($result);
                    //$stmt->close();
                    //$dbc->close();
                    return $result;
                }
            }
            //must have failed...
            return NULL;
    }

在 DBHANDLER 之外

$query = "SELECT error_desc FROM nt_errors WHERE error_code = :ERROR_CODE LIMIT 1";
            //array: holds parameters for the query.
            $params = array(
            ':ERROR_CODE' => $code
            );
            $result = $db->db_query_select($query, $params);
            if ($result == NULL){
                $errorText = 'ERROR: Failed to retrieve error';
            }
            else{
                //var_dump($result);
                $errorText = $result['error_desc'];

PDO 不仅比 mysqli 用户友好得多,而且没有任何此类讨厌的缺点。所以我强烈建议使用PDO而不是mysqli。

使用 DO,您所追求的功能应该像这样简单

function run($sql, $args = NULL)
{
    $pdo = ...;//your means of getting the connection variable
    $stmt = $pdo->prepare($sql);
    $stmt->execute($args);
    return $stmt;
}

获取函数的结果后,您可以链接一个提取方法到它的调用,在您的例子中是fetchColumn()。

鉴于您的代码主要是过程性的,让我向您推荐一个非常 simple PDO wrapper 我写的。所以完整的代码是:

$sql = "SELECT error_desc FROM nt_errors WHERE error_code = ?";
$errorText = DB::run($sql,[$code])->fetchColumn();
if (!$errorText){
    $errorText = 'ERROR: Failed to retrieve error';
}

此处 DB class 是 dbConnect() 函数的更好替代品,运行() 方法是 db_query_select() 的替代品,实际上可用于任何查询类型,包括插入、更新或任何东西。