【发布时间】:2015-05-07 07:12:51
【问题描述】:
当我使用时
WHERE id_prekes = :prekes_id
AND tiekejas = :tiekejas
$prekes_id = 3;
$tiekejas = Silberauto,UAB;
我收到错误(SQLSTATE[HY093]: Invalid parameter number: number),但是当我使用时
WHERE id_prekes = 3
AND tiekejas = "Silberauto, UAB"
错误消失了。我的脚本:
<?php
$tiekejas = $_GET['tiekejas'];
$prekes_id = $_GET['prekes_id'];
echo // "<script type='text/javascript'>alert('$tiekejas');</script>";
$username='root';
$password='pass';
try {
$conn = new PDO('mysql:host=localhost;dbname=univer', $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare('SELECT vnt_kaina
FROM tiekeju_prekes
INNER JOIN tiekejai ON tiekeju_prekes.id_tiekejo = tiekejai.id
WHERE id_prekes = :prekes_id
AND tiekejas = :tiekejas
');
$stmt->bindParam(':tiekejas', $tiekejas, PDO::PARAM_INT);
$stmt->bindParam(':prekes_id', $prekes_id, PDO::PARAM_INT);
$stmt->execute(array('tiekejas' => $tiekejas,'prekes_id' => $prekes_id));
while($row = $stmt->fetch()) {
echo $row['vnt_kaina']." LT ";
}
} catch(PDOException $e) {
echo 'KLAIDA: ' . $e->getMessage();
}
【问题讨论】:
-
$tiekejas和$prekes_id中有什么?echo他们。 -
$tiekejas = Silberauto, UAB $prekes_id = 3;
-
很可能是因为
$stmt->bindParam(':tiekejas', $tiekejas, PDO::PARAM_INT);中的PARAM_INT尝试使用$stmt->bindParam(':tiekejas', $tiekejas, PDO::PARAM_STR);,因为您正在处理字符串"Silberauto, UAB"@shizaa 请参阅手册php.net/manual/en/pdostatement.bindparam.php -
谢谢!我找了 5 天
-
不客气@shizaa