为了使区别更清楚,我们可以查看代码示例:
- < ?php
- $name = 'collins';
- $sql = 'INSERT INTO feeds (name) VALUES (:name)';
- $update = $conn -> prepare($sql);
- $update -> bindParam(':name', $name);
- $update -> execute();
- < ?php
- $name = 'collins';
- $sql = 'INSERT INTO feeds (name) VALUES (:name)';
- $update = $conn -> prepare($sql);
- $update -> bindValue(':name', $name);
- $update -> execute();
-
- $a = 'good boy';
- $b = &$a; // $a and $b both equal "good boy"
- $b = "bad boy"; // $a and $b both equal "bad boy"
- echo "$a"; // echos "bad boy"
因此,即使在 bindParam() 方法被调用后,我们仍然可以更改绑定到参数的变量的值,如下所示:
- $name = 'collins';
- $sql = 'INSERT INTO feeds (name) VALUES (:name)';
- $update = $conn -> prepare($sql);
- $update -> bindParam(':name', $name);
- $name = 'john';
- $update -> execute(); // execute with john inserted into the column "name"
- < ?php
- $sql = 'INSERT INTO feeds (numbers) VALUES (:number)';
- $update = $conn -> prepare($sql);
- $update -> bindValue(':number', 100);
- $update -> execute();
- < ?php
- $sql = 'INSERT INTO feeds (names) VALUES (:name)';
- $update = $conn -> prepare($sql);
- $update -> bindValue(':name', 'collins');
- $update -> execute();
“致命错误:无法通过引用传递参数 2”