简体   繁体   English

mysqli 准备 - 将结果存储到数组中?

[英]mysqli prepared - store results into array?

I want to optimize this section of code a bit to use an array such as $_SESSION['user']= $arr;.我想稍微优化一下这部分代码以使用诸如 $_SESSION['user']= $arr; 之类的数组。

// Store user db info in session for use
$stmt = $mysqli->prepare("SELECT id,user,pass,email,timezone,lastIP,currIP,dtLastLogin,dtCurrLogin FROM test_users WHERE user = ?");
// bind params
$stmt->bind_param('s', $user);
// execute prepared statement
$stmt->execute();
// Bind result variables
$stmt->bind_result($_SESSION['id'], $_SESSION['user'], $_SESSION['pass'], $_SESSION['email'], $_SESSION['timezone'], $_SESSION['lastIP'], $_SESSION['currIP'], $_SESSION['dtLastLogin'], $_SESSION['dtCurrLogin']); 
// fetch values
$stmt->fetch();     
// close statement
$stmt->close();

I tried using :我尝试使用:

$rs = $stmt->get_result();
$arr = $rs->fetch_all(MYSQLI_ASSOC);
// close statement
$stmt->close();
//store array into session
$_SESSION['user']= $arr;

but I received a Call to undefined method mysqli_stmt::get_result().但我收到了对未定义方法 mysqli_stmt::get_result() 的调用。 I have php 5.3.8 and MySQL 5.1.70-cll running.我正在运行 php 5.3.8 和 MySQL 5.1.70-cll。

mysqli_stmt::get_result is only available if you are running the MySQL native driver ( mysqlnd ). mysqli_stmt::get_result仅在您运行 MySQL 本机驱动程序 ( mysqlnd ) 时mysqlnd

This is documented in the manual page for the method .这在方法的手册页中有记录


To clarify:澄清:

There are three ways of accessing a MySQL database from PHP: the ancient mysql functions, the modern mysqli functions/class, and the PDO mysql extension.从 PHP 访问 MySQL 数据库有三种方式:古老的mysql函数、现代的mysqli函数/类和PDO mysql扩展。

All three of these interact with the database in the same way, using the library called libmysqlclient .所有这三个都以相同的方式与数据库交互,使用名为libmysqlclient的库。 Properly speaking, this is not part of PHP.正确地说,这不是 PHP 的一部分。 It is a C library, which PHP uses.它是一个 C 库,PHP 使用它。

In PHP 5.3, however, the mysqlnd driver was introduced.然而,在 PHP 5.3 中,引入了mysqlnd驱动程序。 This is a native part of PHP (that's what the n stands for).这是 PHP 的本机部分(这就是n代表的意思)。 In 5.3, it needs to be installed deliberately.在5.3中,需要特意安装。 From 5.4, it is the default way to access MySQL.从 5.4 开始,它是访问 MySQL 的默认方式。

So to get it working, either install PHP 5.4 or compile PHP 5.3 with the options given in the installation page for mysqlnd .因此,要使其正常工作,请安装 PHP 5.4 或使用mysqlnd安装页面中给出的选项编译 PHP 5.3。

In the meantime, your method is probably the best to get the data.同时,您的方法可能是获取数据的最佳方法。 The only other way would be to use PDO instead, which might offer a nicer syntax.唯一的另一种方法是改用 PDO,这可能会提供更好的语法。 This, for instance, would be possible:例如,这将是可能的:

$stmt = $dbh->prepare("SELECT id,user,pass,email,timezone,lastIP,currIP,dtLastLogin,dtCurrLogin FROM test_users WHERE user = :user");
$stmt->bindParam(':user', $user);
$stmt->execute();

$_SESSION['user'] = $stmt->fetch(PDO::FETCH_ASSOC);

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

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