php-pdo:两次使用参数时参数号无效?

42fyovps  于 2021-06-20  发布在  Mysql
关注(0)|答案(3)|浏览(423)

我有一个问题,当我两次使用位置参数时,我不能在pdo(php/mysql)中使用sql语句:

SELECT `ID` FROM `_LOGIN_` WHERE `LoginName` = :loginName AND `sha512`= SHA2(CONCAT(:pw, (SELECT `salt` FROM `_LOGIN_` WHERE `LoginName` = :loginName)), 512)

如您所见,我使用“:loginname”两次。因此出现以下错误消息:

PHP Fatal error:  Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number in ...

我是否遗漏了什么,或者是否有其他方法来准备语句,以便可以多次使用参数?
下面是要复制的完整代码:

<!DOCTYPE html>
<html>
  <head>
    <title>pdo</title>   

    <meta   charset = "utf-8" />
  </head>

  <body>
      <h1>PDO Prepare</h1>

      <!--
      DB:

DROP   DATABASE IF EXISTS `pdoTestDB`;
CREATE DATABASE           `pdoTestDB`;
ALTER  DATABASE           `pdoTestDB` DEFAULT CHARACTER SET 'utf8' DEFAULT COLLATE 'utf8_general_ci';

CREATE TABLE `_LOGIN_` (
 `ID`           int(11)      NOT NULL
,`LoginName`    TEXT         NOT NULL 
,`SALT`         varchar(  6) NOT NULL 
,`sha512`       varchar(128) NOT NULL 
,`registerTS`   TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP 
,`lastLoginTS`  TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP 
, PRIMARY KEY (`ID`)
) ;

SELECT @SALT:=SUBSTRING(MD5(RAND()) FROM 1 FOR 6);

INSERT INTO `_LOGIN_`
(`ID`, `LoginName`, `salt`, `sha512`                                     , `registerTS`         ) VALUES
(   1, 'muma'      , @SALT, SHA2(CONCAT('123', @SALT), 512), '2018-06-04'         );
      -->

<?php
$PDOcharset = 'utf8mb4';
// set data source name:
$dsn = "mysql:host=localhost;dbname=pdoTestDB;charset=$PDOcharset";
$opt = [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES   => false,
];
$pdo = new PDO($dsn, "santisPHP", "123", $opt);

// the sql
$sql = "SELECT `ID` FROM `_LOGIN_` WHERE `LoginName` = :loginName AND `sha512`= SHA2(CONCAT(:pw, (SELECT `salt` FROM `_LOGIN_` WHERE `LoginName` = :loginName)), 512)";

$stmt = $pdo->prepare($sql);

$paramsAssoc = ['loginName' => "muma", 'pw' => "123"];
        //echo $paramsAssoc;
var_dump($stmt);
var_dump($paramsAssoc);

$result = $stmt->execute($paramsAssoc);

$fetched = $stmt->fetch();

echo "stmt: ";
var_dump($stmt);
echo "<br />";

echo "result: ";
var_dump($result);
echo "<br />";

echo "fetched: ";
var_dump($fetched);
echo "<br />";
?>

  </body>

</html>
vulvrdjw

vulvrdjw1#

pdo不允许在每个查询中多次使用同一参数标识符。您需要在查询中更改标识符的名称,然后在参数中添加另一个匹配的名称。
像这样:

// the sql
$sql = "SELECT `ID` FROM `_LOGIN_` WHERE `LoginName` = :loginName1 AND `sha512`= SHA2(CONCAT(:pw, (SELECT `salt` FROM `_LOGIN_` WHERE `LoginName` = :loginName2)), 512)";

$stmt = $pdo->prepare($sql);

$loginName = 'muma';
$pw = '123';

$stmt->bindParam(":loginName1",     $loginName);
$stmt->bindParam(":loginName2",     $loginName);
$stmt->bindParam(":pw",             $pw);

//echo $paramsAssoc;
var_dump($stmt);
//var_dump($paramsAssoc);

$result = $stmt->execute();

$fetched = $stmt->fetch();

我更喜欢像上面显示的那样绑定我的参数,但是使用您的方法我相信您也可以做到这一点-同样的原则:

$paramsAssoc = ['loginName1' => "muma", 'pw' => "123", 'loginName2' => "muma"];
$result = $stmt->execute($paramsAssoc);
ulmd4ohb

ulmd4ohb2#

根据lowèu rents对类似问题的回答,在您的情况下,您可以执行以下操作:
首先,在查询之前,执行另一个查询来定义 loginName 作为一个 User-Defined Variable :

$stmt = $pdo->prepare("SET @loginName = :loginName");
$stmt->bindValue(":loginName", "muma", PDO::PARAM_STR);
$stmt->execute();

然后,在查询中,替换所有 :loginName 具有现在定义的 @loginName :

// the sql
$sql = "SELECT `ID` FROM `_LOGIN_` WHERE `LoginName` = @loginName AND `sha512`= SHA2(CONCAT(:pw, (SELECT `salt` FROM `_LOGIN_` WHERE `LoginName` = @loginName)), 512)";

最后,像以前一样执行,但这次 $paramsAssoc 数组不需要 'loginName' => "muma" 因为它已经在mysql示例和查询中定义了,所以变得非常简单:

$paramsAssoc = ['pw' => "123"];
yquaqz18

yquaqz183#

或者,您可以将设置更改为 PDO::ATTR_EMULATE_PREPARES => true . 这将允许您通过在pdo本身而不是mysql服务器上准备语句来多次绑定相同的命名参数。

相关问题