gpt4 book ai didi

php - MySQLi 返回一个带有 bind_result 的数组并且获取不工作

转载 作者:搜寻专家 更新时间:2023-10-30 22:14:06 24 4
gpt4 key购买 nike

我想知道如何重写/修复以下代码。

基本上,我有一个名为 DB 的数据库和一个名为 poems 的表。我想获取 poems 表中具有特定 ID 的所有数据(即谁写了这首诗,它具有的标签,它的内容,它的创建时间等)并安全地存储它在一个数组中,稍后被打印到网页上。

我的托管服务不支持 mysqli_getresult() 所需的 mysqlnd 驱动程序,因此我目前收到错误“Call to undefined method mysqli_stmt::get_result()

$mysqli 是预先编写的数据库连接,使用 new mysqli()

function getpoemdata($mysqli) {
echo "Function started.";
if(isset($_GET['poem_id'])) { // Check if poem_id is set in the URL
$poemid=$_GET['poem_id']; // Get poem_id set in URL
if (!is_int($poemid)) { $poemid = 7; } // Check if poem_id is set an integer
if ($poemid <= 0) { $poemid = 7; } //Check if poem_id is positive- If negative, it is changed to positive
$stmt = $mysqli->prepare("SELECT * FROM `poems` WHERE `poem_id` = ?");
$stmt->bind_param('i', $poemid);
$stmt->execute();
$result = $stmt->get_result();
$returned_data = $returned_data->fetch_array();
while($stmt->fetch()){
echo $returned_data . '<br />';
}
$stmt->free_result();
}
else { //If user simply types "poemview.php" and not "poemview.php?poem_id="
echo "error";
}
}

提前非常感谢您提供的任何帮助 - 我对 PHP 还很陌生,并且在学习过程中尝试学习它 - 每次我尝试做一些新的事情并且需要学习更多的功能时都会有点不知所措.

最佳答案

get_result() 是在 PHP 5.3.0 中引入的,如前所述,需要 mysqlnd。但是,我认为您不需要它。您可以使用 fetch()结合 bind_result 检索数据。可能还有其他方法。

文档中的示例:

$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 150,5";

if ($stmt = $mysqli->prepare($query)) {

/* execute statement */
$stmt->execute();

/* bind result variables */
$stmt->bind_result($name, $code);

/* fetch values */
while ($stmt->fetch()) {
printf ("%s (%s)\n", $name, $code);
}

/* close statement */
$stmt->close();
}

/* close connection */
$mysqli->close();
?>

文档下方的示例显示了如何绑定(bind)到数组。

关于php - MySQLi 返回一个带有 bind_result 的数组并且获取不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17386341/

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