简体   繁体   English

PHP get_result()和fetch_assoc()替代

[英]Php get_result() and fetch_assoc() alternative

I am working on writing a PHP backend program, but my program doesn't work because PHP doesn't support the get_result() and fetch_assoc() function. 我正在编写PHP后端程序,但我的程序无法正常工作,因为PHP不支持get_result()fetch_assoc()函数。

How can I rewrite the code so that they can work in the same way as this function? 如何重写代码,以便它们可以与此功能相同的方式工作?

public function storeUser($userLoginID, $password) {                        

    $uuid = uniqid('', true);
    $hash = $this->hashSSHA($password);
    $encrypted_password = $hash["encrypted"];
    $salt = $hash["salt"];
    $stmt = $this->conn->prepare("INSERT INTO hkcheung.users(unique_id,userLoginID,encrypted_password,salt,created_at) VALUES (?,?,?,?,NOW())");
    $stmt->bind_param("ssss", $uuid, $userLoginID, $encrypted_password, $salt);
    $result = $stmt->execute();
    $stmt->close();
    if ($result) {
        $stmt = $this->conn->prepare("SELECT * FROM hkcheung.users WHERE userLoginID=?");
        $stmt->bind_param("s", $userLoginID);
        $stmt->execute();
        $result = $stmt->get_result();
        $users = $result->fetch_assoc();
        $stmt->close();
        return $users;
    } else {
        return false;
    }
}

add below function to your php file 将以下功能添加到您的php文件

function get_result( $Statement ) {
    $RESULT = array();
    $Statement->store_result();
    for ( $i = 0; $i < $Statement->num_rows; $i++ ) {
        $Metadata = $Statement->result_metadata();
        $PARAMS = array();
        while ( $Field = $Metadata->fetch_field() ) {
            $PARAMS[] = &$RESULT[ $i ][ $Field->name ];
        }
        call_user_func_array( array( $Statement, 'bind_result' ), $PARAMS );
        $Statement->fetch();
    }
    return $RESULT;
}

and at the get_result line, change it to 并在get_result行将其更改为

$result =  $this->get_result($stmt);

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

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