如何使用store_result()获取数据?

发布于 2024-08-05 22:15:34 字数 505 浏览 4 评论 0原文

我在执行准备好的语句后尝试使用 $stmt->store_result() 时遇到问题。手册上说“从准备好的语句中传输结果集”,但是传输到哪里呢?

理想情况下,我想使用它,一切都很好并且缓冲在内存中,就像 $mysqli->query() 返回一样。

PHP 文档示例使得使用 store_result() 的唯一充分理由似乎是获取行数。

理想情况下,我需要一些可以从准备好的语句中获取整个结果集并对其执行诸如 fetch_rows 之类的操作的东西。我确实通过扩展 mysqli_stmt 类并使用一些向后方法添加 fetch_assoc 函数来破解它,但如果我可以缓冲所有数据,我真的很想删除该黑客进入记忆。

所以我想我的简短问题是,我知道 store_result() 的作用,但你如何使用它?

(老实说,如果它在防止 SQL 注入攻击、消除变量和使我的 SQL 看起来非常漂亮,带有所有这些问号。)

I'm having trouble trying to use $stmt->store_result() after executing a prepared statement. The manual says "Transfers a result set from a prepared statement", but to where?

I would ideally like to use it, all nice and buffered in memory, like $mysqli->query() returns.

The PHP documentation examples make it seem like the only good reason to use store_result() is to get the number of rows.

Ideally, I need something that can get the entire result set from a prepared statement and do something like fetch_rows on it. I DID hack the mysqli_stmt class by extending it and adding a fetch_assoc function using some backwards methods, but I would really like to remove that hack if I can get all my data buffered into memory.

So I guess my short question is, I know what store_result() does, but how do you USE it?

(And honestly, I wouldn't even be using $mysqli->prepare() to get a stmt, if it wasn't so damn useful in preventing SQL injection attacks, sterilizing variables, and making my SQL look so nice and pretty with all those question marks.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲喜皆因你 2024-08-12 22:15:34

store_result() 唯一做的事情就是将结果从 MySQL 移动到 PHP 内存中。它不是很有用,因为您仍然需要将结果获取到 PHP 变量中。

您正在寻找的是get_result()。该函数将从 MySQL 获取结果并将其存储在 PHP 内存中,另外返回一个熟悉的 mysqli_result 对象。

$stmt = $mysqli->prepare('SELECT ? as col');
$stmt->bind_param('s', $variable);
$stmt->execute();

// returns mysqli_result object
$result = $stmt->get_result();

// loop to get associative rows
foreach ($result as $row) {
    echo $row['col'];
}

The only thing that store_result() does is it moves the result from MySQL into PHP memory. It's not very useful since you still need to fetch the results into PHP variables.

What you are looking for is get_result(). This function will fetch results from MySQL and store them in PHP memory, additionally returning a familiar mysqli_result object.

$stmt = $mysqli->prepare('SELECT ? as col');
$stmt->bind_param('s', $variable);
$stmt->execute();

// returns mysqli_result object
$result = $stmt->get_result();

// loop to get associative rows
foreach ($result as $row) {
    echo $row['col'];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文