0

从命令行运行时的当前错误是“调用非对象上的成员函数 bindParam()”,我发现这是变量 $orderPO 的问题。有些东西不喜欢非数字字符,这导致我使用了也不起作用的 bindParam PARAM_STR 业务。数据库字段都是 varchar 50。

我的搜索技巧让我失望了。我知道这必须在某个地方发布大约一百万次,但我似乎找不到它。如果有人有更好的主意,我完全愿意以另一种方式这样做。

当前尝试代码:

try
{
    $orderNum = '123456';
    $orderPO = '123456-A';


    $dbh = new PDO("mysql:host=localhost;dbname=dbname", 'someuser', 'somepass');
    $stm = $dbh->prepare("insert into some_table (order_number, order_po)");
    $stm->bindParam(':order_number', $orderNum, PDO::PARAM_STR);
    $stm->bindParam(':order_po', $orderPO, PDO::PARAM_STR);
    $stm->execute();
    print_r($stm);
    print_r($dbh);
    $arr = $stm->errorInfo();
    print_r($arr);
    $stm->closeCursor();
    $dbh = null;
}
catch(PDOException $e)
{
    echo $e->getMessage();
}
4

3 回答 3

2

为了使用 PDO 绑定参数,您需要使用占位符,如下所示:

$stm = $dbh->prepare("
    INSERT INTO `some_table` SET
        `order_number` = :order_number,
        `order_po` = :order_po
");
$stm->bindParam(':order_number', $orderNum, PDO::PARAM_STR);
$stm->bindParam(':order_po', $orderPO, PDO::PARAM_STR);

:请注意在命名占位符之前包含字符。我还在您的查询中添加了列名。

进一步阅读并查看示例:PDO bindParam

于 2012-06-21T16:24:44.177 回答
0

正确的语法是

$stm = $dbh->prepare("insert into some_table (order_number, order_po) VALUES (?, ?)");
$stm->bindParam(1,$orderNum);
$stm->bindParam(2,$orderPO);

包括问号,bindParam 调用中的数字是指您将参数绑定到哪个问号

于 2012-06-21T16:23:53.313 回答
0

您正在尝试使用 bindparam,但绑定参数匹配?不是游标:. 您没有包含任何参数或值。

此外,您在查询中缺少 VALUES 语句,这导致查询失败。这就是为什么您会得到“在非对象上调用成员函数 bindParam()”的原因

要使用 :value 语法,请使用 bindValue,而不是 bindParam。要使用 bindParam,请将 :value 切换为 ? 在您的查询中并按顺序编号它们是您的执行数组。

try
{

    $orderNum = '123456';
    $orderPO = '123456-A';


    $dbh = new PDO("mysql:host=localhost;dbname=dbname", 'someuser', 'somepass');
    $stm = $dbh->prepare("insert into some_table (order_number, order_po) VALUES (:order_number, :order_po)");
    $stm->bindvalue(':order_number', $orderNum, PDO::PARAM_STR);
    $stm->bindvalue(':order_po', $orderPO, PDO::PARAM_STR);
    $stm->execute();
    print_r($stm);
    print_r($dbh);
    $arr = $stm->errorInfo();
    print_r($arr);
    $stm->closeCursor();
    $dbh = null;
}
catch(PDOException $e)
{
    echo $e->getMessage();
}
于 2012-06-21T16:24:34.400 回答