php PDO绑定问题

时间:2011-01-26 21:29:17

标签: php pdo

我使用php PDO作为db连接器。我在绑定电子邮件地址时遇到问题 说我有一个sql

$sql = UPDATE user set email = :email where uid = 10

$smt = $dbh->prepare($sql);

$smt->bindParam(':email', 'xyz@gmail.com');

PDO将在@之后留下所有东西。

感谢名单

2 个答案:

答案 0 :(得分:5)

bindParam用于绑定变量,而不是值。我想你想要做的是改用bindValue

答案 1 :(得分:-1)

define("SQLHOST", "127.0.0.1");
define("SQLUSER", "user");
define("SQLPASS", "password");
define("SQLSGBD", "database");

try {
    $dbh = new PDO('mysql:host=' . SQLHOST . ';dbname=' . SQLSGBD . ';charset=UTF8', SQLUSER, SQLPASS);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    var_dump('Connection failed: ' . $e->getMessage());
}

$emailList = ['xyz@gmail.com', 'abc@gmail.com'];
$stmt = $dbh->prepare("UPDATE user set email = ? where uid = 10");
foreach ($emailList as $key => $email) {
    $stmt->bindParam(1, $email, PDO::PARAM_STR);
    $stmt->execute();
    $obj = $stmt->fetchall(PDO::FETCH_ASSOC);
    if (count($obj) !== 0) {
        /* whatever */
    }
}