【问题标题】:PDO PHP bindParam() repeated use of same parametersPDO PHP bindParam() 重复使用相同的参数
【发布时间】:2018-01-09 18:58:06
【问题描述】:

昨天我决定学习PDO并将我们的服务器php重写为PDO。

在重写代码时,我突然想到的是需要对我已经使用过的相同参数重复使用 bindParam。

这是一个例子:

$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $dbh->beginTransaction();
    $stmt = $dbh->prepare("INSERT INTO Products(productID,numOfLikes) VALUES (:productID,0) ON DUPLICATE KEY UPDATE productID = productID;");
    $stmt->bindParam(":productID",$productID);
    $stmt->execute();

    if($customerID !== 0){  
        //*****Check, if customerID is in the Database, else add the customerID to the Database.
        $stmt = $dbh->prepare("INSERT INTO Customers(customerID) VALUES (:customerID) ON DUPLICATE KEY UPDATE customerID = customerID;");
        $stmt->bindParam(":customerID",$customerID);
        $stmt->execute();

        //*****if customerID and productID are NOT registered together ,then register and add +1 to productID numOfLikes
        $stmt = $dbh->prepare("SELECT customerID, productID FROM CustomerProducts WHERE productID = :productID AND customerID = :customerID");          
        $stmt->bindParam(":productID",$productID);
        $stmt->bindParam(":customerID",$customerID);
        $stmt->execute();

        if ($stmt->rowCount() == 0) {
            //echo "added";
            $stmt = $dbh->prepare("INSERT INTO CustomerProducts(customerID, productID) Values (:customerID,:productID)");
            $stmt->bindParam(":customerID",$customerID);
            $stmt->bindParam(":productID",$productID);
            $stmt->execute();

            $stmt = $dbh->prepare("UPDATE Products SET numOfLikes = numOfLikes + 1 WHERE productID = :productID");
            $stmt->bindParam(":productID",$productID);
            $stmt->execute();  
        }else {
            //echo "removed";
            $stmt = $dbh->prepare("DELETE FROM CustomerProducts WHERE productID = ".$productID." AND customerID = ".$customerID);
            $stmt->bindParam(":customerID",$customerID);
            $stmt->bindParam(":productID",$productID);
            $stmt->execute();

            $stmt = $dbh->prepare("UPDATE Products SET numOfLikes = numOfLikes - 1 WHERE productID = ".$productID);
            $stmt->bindParam(":productID",$productID);
            $stmt->execute();  
        }
    }
    $dbh->commit();

有没有办法以“更漂亮的方式”编写它? 你能看到里面的任何流动吗?我将不胜感激。

注意:此代码将在不久的将来用于生产。

【问题讨论】:

  • 顺便说一句,您忘记在 DELETE 语句中使用参数。上次更新时相同。
  • 好一个。谢谢我的朋友!
  • 如果你想设置新值而不是现有值,你应该使用ON DUPLICATE KEY UPDATE customerID = VALUES(customerID)
  • 我不想设置新的而不是现有的。这是我从stackoverflow上的一个人那里看到的一个技巧。它说插入,但是,如果已经存在,则不快速执行任何操作。
  • 为什么不直接使用INSERT IGNORE呢?使用INSERT...ON DUPLICATE KEY UPDATE 执行插入,然后可能执行更新。

标签: php mysql pdo bindparam


【解决方案1】:

是的,有……

您可以将bindParam 作为数组提供给execute 函数...

类似这样的:

$statement->execute([
    ':username'=> $username,
    ':password'=> $password
]);

它只在一个语句中使用了bindParamexecute,在我看来它看起来更干净。

【讨论】:

  • 这更像是使用bindValue(),因为你可以传递一个表达式而不是需要传递一个左值。无论如何,是的,我也总是对execute() 使用数组参数。
  • 是的,类似的。
  • 但除此之外,我仍然需要在每个查询中一遍又一遍地使用 productID 和 customerID。我可以将绑定存储在某个地方并每次调用它吗?
  • @ArielEstrin 我认为没有必要。
【解决方案2】:

是的,您可以通过像这样定义 mySql 用户变量来绕过重复变量:

$psVars = $dbh->prepare("SET @pid = :productID;");

$psVars->bindParam(':productID', $productID);

$psVars->执行();

然后,在后续语句中,只需使用@pid 代替绑定参数

【讨论】:

    猜你喜欢
    • 2014-08-20
    • 1970-01-01
    • 2018-11-12
    • 2012-11-04
    • 1970-01-01
    • 1970-01-01
    • 2014-10-24
    • 2013-02-15
    • 2011-04-09
    相关资源
    最近更新 更多