【发布时间】:2017-04-03 12:47:13
【问题描述】:
我正在从 mysqli 语法切换到 PDO 并且有一些疑问:
在我使用这个之前(绑定int、字符串、十进制值的例子):
$stmt = $conn->prepare("INSERT INTO MyGuests (firstname, id, value) VALUES (?, ?, ?)");
$stmt->bind_param("sid", $firstname, $id, $value);
$stmt->execute();
使用 PDO 我应该使用这个:(这里 param decimal 已经不存在,更不用说我必须为 binging 写多行)
$stmt = $conn->prepare("INSERT INTO MyGuests (firstname, id, value) VALUES (?, ?, ?)");
$stmt->bindParam(1, $firstname, PDO::PARAM_STR);
$stmt->bindParam(2, $id, PDO::PARAM_INT);
$stmt->bindParam(3, $value, PDO::PARAM_STR);//no decimal type!
$stmt->execute();
我应该“忘记”类型并这样做吗?
$stmt = $conn->prepare("INSERT INTO MyGuests (firstname, id, value) VALUES (?, ?, ?)");
$stmt->execute([$firstname, $id, $value]);
这种情况下 int 和 decimal 怎么会失败?
【问题讨论】:
-
事物
PDO::PARAM_更多地用于(不仅)生成带或不带引号的正确 sql 查询。浮点数/小数不能像整数2那样输入,它们必须在实际执行的查询中加上'1.1'之类的引号,所以使用PDO::PARAM_STR就可以了。或者像你说的那样忘记它,只使用execute()。