我如何正确使用mysqli_stmt :: bindParam()

时间:2015-02-25 15:05:49

标签: php mysql sqlbindparameter

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

我一直在关注以下网址的详细信息: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();
?>

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

我已将下面的代码从我的主程序中取出并将其放入其自己的文件中,以查看是否可以隔离该问题,到目前为止,它已经存在。我已经复制了一个剪切的输入数据,以重新创建与实际数据相同的条件,而且,我做得对,因为我得到了同样的错误。

我收到了错误消息:Call to undefined method 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

2 个答案:

答案 0 :(得分:0)

Mysqli在PDO中bind_param使用了bindParam

答案 1 :(得分:0)

首先,您在第一段中提供的链接属于PDO。但是在标题中你说msqli并且在代码中你创建了mysqli。

所以你需要阅读文档:

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

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