SQLSTATE [HY093]:参数号无效:数字

时间:2014-04-29 23:43:31

标签: php mysql pdo

当我使用

WHERE id_prekes = :prekes_id
AND tiekejas = :tiekejas

$ prekes_id = 3;

$ tiekejas = Silberauto,UAB;

我收到错误(SQLSTATE [HY093]:参数号无效:数字),但是当我使用时

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();
}

1 个答案:

答案 0 :(得分:0)

使用:

$stmt->bindParam(':tiekejas', $tiekejas, PDO::PARAM_STR);
$stmt->bindParam(':prekes_id', $prekes_id, PDO::PARAM_INT);

或者

$stmt->execute(array('tiekejas' => $tiekejas,'prekes_id' => $prekes_id));

同时注意PDO::PARAM_ - $tiekejas是一个字符串,因此您应该使用PDO::PARAM_STR而不是PDO::PARAM_INT