gpt4 book ai didi

php - 合并两个准备好的语句不起作用

转载 作者:行者123 更新时间:2023-11-29 10:39:21 26 4
gpt4 key购买 nike

首先,根据另一个SO post ,我尝试将这两个语句合并为一个。

<?php
$conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, 1);
$sql = "UPDATE users SET pass = :password WHERE usrn = :id;
SELECT prim FROM users WHERE usrn = :id;";
$stmt = $conn->prepare($sql);
$stmt->bindParam(":id", $_SESSION["idPersist"]);
$stmt->bindParam(":password", password_hash($_POST["password"], PASSWORD_DEFAULT));
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC); //// line 71
?>

但是,这不断抛出错误: fatal error :未捕获的 PDOException:SQLSTATE[HY000]:第 71 行出现常规错误

我找不到此问题的任何相关解决方案,因此我决定简单地将这两个语句分开。

<?php
$sql = "UPDATE users SET pass = :password WHERE usrn = :id";
$stmt = $conn->prepare($sql);
$stmt->bindParam(":id", $_SESSION["idPersist"]);
$stmt->bindParam(":password", password_hash($_POST["password"], PASSWORD_DEFAULT));
$stmt->execute();
$sql = "SELECT prim FROM users WHERE usrn = :id";
$stmt = $conn->prepare($sql);
$stmt->bindParam(":id", $_SESSION["idPersist"]);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
$_SESSION["session"] = $result["prim"];
?>

但是 var_dump($result) 返回 Bool(false),因此显然在获取结果并将其存储为变量时有些东西无法正常工作,它看来,在这两种情况下。

我是 PHP 和 MySQL 的新手,所以我现在很茫然。

最佳答案

改变这个,

$sql  = "SELECT prim FROM users WHERE usrn = :id";
$stmt = $conn->prepare($sql);
$stmt->bindParam(":id", $_SESSION["idPersist"]);
$result = $stmt->fetch(PDO::FETCH_ASSOC);
$_SESSION["session"] = $result["prim"];

对此,

$sql  = "SELECT prim FROM users WHERE usrn = :id";
$stmt = $conn->prepare($sql);
$stmt->bindParam(":id", $_SESSION["idPersist"]);
$stmt->execute(); // Your problem
$result = $stmt->fetch(PDO::FETCH_ASSOC);
$_SESSION["session"] = $result["prim"];

您错过了查询的执行。

关于php - 合并两个准备好的语句不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45810415/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com