【问题标题】:SQLSTATE[HY093]: Invalid parameter number: numberSQLSTATE[HY093]:无效的参数号:number
【发布时间】: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-&gt;bindParam(':tiekejas', $tiekejas, PDO::PARAM_INT); 中的PARAM_INT 尝试使用$stmt-&gt;bindParam(':tiekejas', $tiekejas, PDO::PARAM_STR);,因为您正在处理字符串"Silberauto, UAB" @shizaa 请参阅手册php.net/manual/en/pdostatement.bindparam.php
  • 谢谢!我找了 5 天
  • 不客气@shizaa

标签: php mysql pdo


【解决方案1】:

使用任一:

$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

【讨论】:

    猜你喜欢
    • 2023-01-09
    • 2016-03-04
    • 2019-09-28
    • 2013-08-04
    • 2016-12-04
    • 2016-01-13
    相关资源
    最近更新 更多