gpt4 book ai didi

php - 如何正确使用 mysqli_stmt::bindParam()

转载 作者:行者123 更新时间:2023-11-29 22:55:58 24 4
gpt4 key购买 nike

我很难理解如何正确使用bindParam();

我一直在关注以下 URL 的详细信息:http://php.net/manual/en/pdo.prepared-statements.php其中的示例之一如下所示:

<?php
$stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value) VALUES (?, ?)");
$stmt->bindParam(1, $name);
$stmt->bindParam(2, $value);

// insert one row
$name = 'one';
$value = 1;
$stmt->execute();

// insert another row with different values
$name = 'two';
$value = 2;
$stmt->execute();
?>

这个示例显然不完整,但是,它显示了足够的细节,我应该能够重现相同的结果。

我已将下面的代码从主程序中取出,并将其放入自己的文件中,看看是否可以隔离问题,到目前为止,它已经隔离了。我复制了输入数据的片段,以重新创建与真实数据相同的条件,好吧,我做对了,因为我遇到了相同的错误。

我收到错误消息:调用未定义的方法 mysqli_stmt::bindparam() 这让我相信 stmt 出了问题。因此,我放入了一些调试代码来查看 stmt 内部的内容。它似乎是一个对象,因此,它已定义,到目前为止,没有任何 SQL 错误提示 SQL 语法错误,一切都连接起来,并且似乎运行正常,并且该对象在我看来已定义,但是,一分钟它会使用它,它是未定义的。

我很茫然,因为我尽我所能地遵循了这个例子。看来 bindParam() 在设置时使用了引用,因此我提前创建了变量,以便它可以使用。

有一些我不明白的事情,并且文档的解释不足以让我理解。

以下是我正在使用的测试脚本,它是完整的文件,减去了一些明显的东西。

<?php
$servername = "localhost";
$username = "-----";
$password = "--------";
$dbname = "-----";
$EN["Data"]["Episode"][1]["id"] = "1";
$EN["Data"]["Episode"][1]["seasonid"] = "14";
$EN["Data"]["Episode"][1]["seriesid"] = "143";
$EN["Data"]["Episode"][1]["SeasonNumber"] = "1";
$EN["Data"]["Episode"][1]["EpisodeName"] = "1";
$EN["Data"]["Episode"][2]["id"] = "2";
$EN["Data"]["Episode"][2]["seasonid"] = "14";
$EN["Data"]["Episode"][2]["seriesid"] = "143";
$EN["Data"]["Episode"][2]["SeasonNumber"] = "1";
$EN["Data"]["Episode"][2]["EpisodeName"] = "2";

echo "<pre>";
print_r($EN);
echo "</pre>";

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (! $conn->set_charset("utf8mb4")) {
die("Error loading character set utf8: " . $conn->error);
}
$stmt = $conn->prepare( "INSERT INTO TVEpisodes
(id, seasonid, seriesid, SeasonNumber, EpisodeName)
VALUES (?, ?, ?, ?, ?)" );

if ($stmt == FALSE) { /*<--- didn't fail here. */
echo "Error: " . $sql . "<br>" . $conn->error;
exit(); // should not happen at this point, mabye...
}
echo "<pre>";
var_dump($stmt); /*<--- both show I have data.*/
print_r($stmt);
echo "</pre>";

$episode_id = " ";
$episode_seasonid = " ";
$episode_seriesid = " ";
$episode_SeasonNumber = " ";
$episode_EpisodeName = " ";

$stmt->bindParam(1, $episode_id); /*<------ fails here.*/
echo "<pre>";
var_dump($stmt);
print_r($stmt);
echo "</pre>";
$stmt->bindParam(2, $episode_seasonid);
$stmt->bindParam(3, $episode_seriesid);
$stmt->bindParam(4, $episode_SeasonNumber);
$stmt->bindParam(5, $episode_EpisodeName);
foreach ($EN["Data"]["Episode"] as &$episode) {
echo "<pre>->>>";
print_r($episode);
echo "<<<<- </pre>";
$episode_id = $episode["id"];
$episode_seasonid = $episode["seasonid"];
$episode_seriesid = $episode["seriesid"];
$episode_SeasonNumber = $episode["SeasonNumber"];
$episode_EpisodeName = $episode["EpisodeName"];
if ($stmt->execute() != TRUE) {
echo "Error: " . $sql . "<br>" . $conn->error;
exit(); // should not happen at this point, mabye...
}
}
?>

这是显示输出值的输出页面。

Array
(
[Data] => Array
(
[Episode] => Array
(
[1] => Array
(
[id] => 1
[seasonid] => 14
[seriesid] => 143
[SeasonNumber] => 1
[EpisodeName] => 1
)

[2] => Array
(
[id] => 2
[seasonid] => 14
[seriesid] => 143
[SeasonNumber] => 1
[EpisodeName] => 2
)

)

)

)
object(mysqli_stmt)[2]
public 'affected_rows' => null
public 'insert_id' => null
public 'num_rows' => null
public 'param_count' => null
public 'field_count' => null
public 'errno' => null
public 'error' => null
public 'error_list' => null
public 'sqlstate' => null
public 'id' => null
mysqli_stmt Object
(
[affected_rows] => 0
[insert_id] => 0
[num_rows] => 0
[param_count] => 5
[field_count] => 0
[errno] => 0
[error] =>
[error_list] => Array
(
)

[sqlstate] => 00000
[id] => 1
)

( ! ) Fatal error: Call to undefined method
mysqli_stmt::bindparam() in /home/-long-path-/test.php on line 45

最佳答案

首先,您在第一段中给出的链接属于 PDO。但在标题中你说的是 mysqli,而在代码中你创建了 mysqli。

因此您需要阅读此处的文档:

http://php.net/manual/en/mysqli-stmt.bind-param.php

$stmt->bind_param('ssss', $episode_seasonid,$episode_seriesid,$episode_SeasonNumber,$episode_EpisodeName); 

关于php - 如何正确使用 mysqli_stmt::bindParam(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28722422/

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