dongzi1959 2015-11-28 07:12
浏览 65

错误PHP MySQL调用未定义的方法mysqli_stmt :: get_result()

I have script like this:

<?php

class DB_Functions {

    private $conn;

    // constructor
    function __construct() {
        require_once 'db_connect.php';
        // connecting to database
        $db = new Db_Connect();
        $this->conn = $db->connect();
    }

    // destructor
    function __destruct() {

    }

    /**
     * Storing new user
     * returns user details
     */
    public function storeUser($name, $email, $password) {
        $uuid = uniqid('', true);
        $hash = $this->hashSSHA($password);
        $encrypted_password = $hash["encrypted"]; // encrypted password
        $salt = $hash["salt"]; // salt

        $stmt = $this->conn->prepare("INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at) VALUES(?, ?, ?, ?, ?, NOW())");
        $stmt->bind_param("sssss", $uuid, $name, $email, $encrypted_password, $salt);
        $result = $stmt->execute();
        $stmt->close();

        // check for successful store
        if ($result) {
            $stmt = $this->conn->prepare("SELECT * FROM users WHERE email = ?");
            $stmt->bind_param("s", $email);
            $stmt->execute();
            $user = $stmt->get_result()->fetch_assoc();
            $stmt->close();

            return $user;
        } else {
            return false;
        }
    }

    /**
     * Get user by email and password
     */
    public function getUserByEmailAndPassword($email, $password) {

        $stmt = $this->conn->prepare("SELECT * FROM pasien WHERE kd_pasien = ?");

        $stmt->bind_param("s", $email);

        if ($stmt->execute()) {
            $user = $stmt->get_result()->fetch_assoc();
            $stmt->close();
            return $user;
        } else {
            return NULL;
        }
    }

    /**
     * Check user is existed or not
     */
    public function isUserExisted($email) {
        $stmt = $this->conn->prepare("SELECT email from users WHERE email = ?");

        $stmt->bind_param("s", $email);

        $stmt->execute();

        $stmt->store_result();

        if ($stmt->num_rows > 0) {
            // user existed 
            $stmt->close();
            return true;
        } else {
            // user not existed
            $stmt->close();
            return false;
        }
    }

    /**
     * Encrypting password
     * @param password
     * returns salt and encrypted password
     */
    public function hashSSHA($password) {

        $salt = sha1(rand());
        $salt = substr($salt, 0, 10);
        $encrypted = base64_encode(sha1($password . $salt, true) . $salt);
        $hash = array("salt" => $salt, "encrypted" => $encrypted);
        return $hash;
    }

    /**
     * Decrypting password
     * @param salt, password
     * returns hash string
     */
    public function checkhashSSHA($salt, $password) {

        $hash = base64_encode(sha1($password . $salt, true) . $salt);

        return $hash;
    }

}

?>

But i got error: Call to undefined method mysqli_stmt::get_result()

Error found on this line:

$user = $stmt->get_result()->fetch_assoc();

I'm trying to make API with this code. Anybody have solution? I have try some solution on stackoverflow but not work for my case. Thanks in advance

  • 写回答

1条回答 默认 最新

  • duanrong0738 2015-11-28 07:58
    关注

    I did a bit of searching. It appears from user comments on the php documentation for the method that you need a mysqlnd driver in order to call the mysqli::get_result() method or mysqli_stmt_get_result() function or else they appear undefined.

    http://php.net/manual/en/mysqli-stmt.get-result.php

    If you do not want to bother with the driver someone shared their own procedural substitute for the mysqli_stmt_get_result() function in the documentation comments.

    Edit: know what, I did a project recently and was trying to get results from a prepared statement and ended up finding this function. It fetches all the rows from a result set.

    // Found from: http://php.net/manual/en/mysqli-stmt.bind-result.php
    // user: nieprzeklinaj at gmail dot com
    function fetch($result)
    {   
        $array = array();
    
        if($result instanceof mysqli_stmt)
        {
            $result->store_result();
    
            $variables = array();
            $data = array();
            $meta = $result->result_metadata();
    
            while($field = $meta->fetch_field())
                $variables[] = &$data[$field->name]; // pass by reference
    
            call_user_func_array(array($result, 'bind_result'), $variables);
    
            $i=0;
            while($result->fetch())
            {
                $array[$i] = array();
                foreach($data as $k=>$v)
                    $array[$i][$k] = $v;
                $i++;
    
                // don't know why, but when I tried $array[] = $data, I got the same one result in all rows
            }
        }
        elseif($result instanceof mysqli_result)
        {
            while($row = $result->fetch_assoc())
                $array[] = $row;
        }
    
        return $array;
    }
    

    And you would use it like this because you would want the first result:

    // instead of $stmt->get_result()->fetch_assoc();
    $rows = fetch($stmt);
    $user = $rows[0]; //null or the first result as an assoc array
    

    It also works for normal queries as well:

    $rows = fetch($db->query("..."));
    

    For the possible solution given on the php documentation I mentioned initially I believe it would be used like:

    // instead of $stmt->get_result()->fetch_assoc();
    $qresult = iimysqli_stmt_get_result($stmt);
    $user = iimysqli_result_fetch_array($qresult);
    
    评论

报告相同问题?

悬赏问题

  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划