Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PDO PHP bindParam()重复使用相同的参数_Php_Mysql_Pdo_Bindparam - Fatal编程技术网

PDO PHP bindParam()重复使用相同的参数

PDO PHP bindParam()重复使用相同的参数,php,mysql,pdo,bindparam,Php,Mysql,Pdo,Bindparam,昨天我决定学习PDO并将我们的服务器php重写为PDO 在重写代码时,我突然想到的是,对于我已经使用过的相同参数,需要重复使用bindParam 以下是一个例子: $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $dbh->beginTransaction(); $stmt = $dbh->prepare("INSERT INTO Products(productID,numOfLi

昨天我决定学习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();
有没有办法用“更漂亮的方式”来写呢? 你能看到里面有什么流动吗。我将非常感谢你的帮助

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

是的,有

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

大概是这样的:

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

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

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

$statement->execute([
    ':username'=> $username,
    ':password'=> $password
]);
$psVars=$dbh->prepare(“SET@pid=:productID;”)

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

$psVars->execute()


然后,在后续语句中,只需使用@pid而不是绑定参数

BTW,您忘记了在DELETE语句中使用参数。上次更新也是一样。好的。谢谢我的朋友!另外,如果要设置新值而不是现有值,则应在重复键更新customerID=VALUES(customerID)上使用
。我不想设置新值而不是现有值。这是我在stackoverflow上看到的一个家伙的把戏。它说的是插入,但是,如果已经存在,就什么也不做。为什么不直接使用
insert IGNORE
?使用
INSERT…ON DUPLICATE KEY UPDATE
执行插入,然后可能执行更新。这更像是使用
bindValue()
,因为您可以传递表达式,而不需要传递左值。无论如何,是的,我也总是使用数组参数来执行()
。是的,类似这样。但除此之外,我仍然需要在每个查询中反复使用productID和customerID。我能把装订好的东西存放在什么地方,每次都叫它吗?@ArielEstrin我觉得没必要。