结合两个准备好的陈述不起作用

时间:2017-08-22 06:33:04

标签: php mysql pdo prepared-statement

首先,根据另一个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: Uncaught PDOException: SQLSTATE[HY000]: General error on line 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的新手,所以我现在感到茫然。

1 个答案:

答案 0 :(得分:1)

改变这一点,

export function initMap()
{
    map = new google.maps.Map(document.getElementById('map'), {
        center: {lat: -34.397, lng: 150.644},
        zoom: 8
    });

    fetch('/data/markers.json')
      .then(function(response){return response.json()})
      .then(plotMarkers);
}

对此,

$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"];

您错过了查询的执行。