gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 21:47:40 25 4
gpt4 key购买 nike

我有这样的脚本:

<?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;
}

}

?>

但我收到错误:调用未定义的方法 mysqli_stmt::get_result()

此行发现错误:

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

我正在尝试使用此代码制作 API。有人有解决办法吗?我在 stackoverflow 上尝试了一些解决方案,但不适用于我的情况。提前致谢

最佳答案

我做了一些搜索。从该方法的 php 文档上的用户评论看来,您需要 mysqlnd 驱动程序才能调用 mysqli::get_result() 方法或 mysqli_stmt_get_result() 函数,否则它们将显示为未定义。

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

如果您不想打扰驱动程序,有人在文档注释中分享了他们自己的 mysqli_stmt_get_result() 函数的程序替代品。

编辑:知道吗,我最近做了一个项目,试图从准备好的语句中获取结果,最终找到了这个函数。它从结果集中获取所有行。

// 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;
}

你会像这样使用它,因为你想要第一个结果:

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

它也适用于普通查询:

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

对于我最初提到的 php 文档中给出的可能解决方案,我相信它的用法如下:

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

关于php - 错误 PHP MySQL 调用未定义的方法 mysqli_stmt::get_result(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33968757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com