0

First, according to another SO post, I tried combining the two statements into one.

<?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
?>

However, this kept throwing the error: Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error on line 71.

I couldn't find any relevant solutions to this issue, so I decided to simply split up the two statements.

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

But a var_dump($result) is returning Bool(false), so obviously something is not working right with fetching the result and storing it as a variable, it seems, in both cases.

I'm new to PHP and MySQL, so I'm at a loss right now.

oldboy
  • 5,729
  • 6
  • 38
  • 86
  • 1
    Try using fetchAll instead Of fetch – Rotimi Aug 22 '17 at 06:34
  • @Akintunde I will try that thx – oldboy Aug 22 '17 at 06:41
  • @Akintunde fetch actually works. i was just missing an execute statement. i guess i've been staring at a screen for too long. i could've sworn i checked for that :/ is there no way to combine the two statements? – oldboy Aug 22 '17 at 06:49
  • Oh lol I didn't even spot that. Because your code is kinda clustered. What do you mean combine? And can't you join the two tables? How is the database schema like – Rotimi Aug 22 '17 at 06:51
  • If you're coding take a rest I make mistakes when I go in to hyper drive and what IDE are you using to code ? – S4NDM4N Aug 22 '17 at 06:52
  • @Akintunde yeah, it is super crowded lol. oh, it's just one single table, but in order to make the code more concise and less repetitive i'd like to combine the two statements and then execute them both somehow, similar to what i first tried doing when it was throwing that error. – oldboy Aug 22 '17 at 06:53
  • @Sand just notepadd++ before i transfer it over to linux where my server is. not a huge fan coding in nano. u?? i did take a break, but i've been at it all day and my eating habits aren't the best lol – oldboy Aug 22 '17 at 06:56
  • Well I use phpStorm very easy it is available for linux but it's not free those syntax errors do occur when the IDE is not that grate. I used to use linux but now I'm windows (for reasons). What you need is a good IDE NetBeans kind of up there with phpStorm but it's all boils down to what human mind see's we do get blind spots when we don't take breaks and let the brain get refreshed so personally I take 10 to 15 m break for ever 1h of work. – S4NDM4N Aug 22 '17 at 07:07
  • @Sand notepadd++ really isn't an IDE, but a simple text editor with extra features (i.e. highlights syntax, autocomplete, etc). when i work, i'm constantly taking smoke breaks about every 30 minutes for a few minutes, which prolly isn't the best type of breaks but i get away from the screen nonetheless. i def become epiphany prone when i walk away. but i just spend wayyy too much time in front of a screen. i'm usually on the PC almost all day everyday. yeah, i'm still on windows too. i'm only using linux to host my server. windows gui is just too good lol. we r gon get in shit if we cont tlkng – oldboy Aug 22 '17 at 07:16
  • Yep the system gonna switch to chat any way good to know there's still good people in here. – S4NDM4N Aug 22 '17 at 07:28
  • @Sand Likewise!! <3 Eventually, I guess hopefully, another mentality will take over – oldboy Aug 22 '17 at 07:41

1 Answers1

1

Change this,

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

To this,

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

You are missing the execution of the query.

S4NDM4N
  • 904
  • 2
  • 11
  • 26
  • NM i guess i did miss it o: i could've sworn i checked for that. is it not possible to combine the 2 queries? – oldboy Aug 22 '17 at 06:42
  • deleting this question now. i don't want any other down votes :/ – oldboy Aug 22 '17 at 06:48
  • 1
    I balanced it no question is stupid even I've asked things like this. – S4NDM4N Aug 22 '17 at 06:51
  • that's exactly how i feel. i rarely ever downvote comments, even ones that are incredibly straightforward. everybody's gotta start somewhere, and everybody makes mistakes, right? i hate that portion of the SO community that relentlessly downvotes stuff. it's still not that hard finding stuff if you know the terms and what not when you're googling or SOing for a solution – oldboy Aug 22 '17 at 06:59