0

I've been staring over this code for quite awhile now and I can't seem to get it to work at all, I have a prepared statement as follow

  $stmt = $conn->prepare('SELECT * FROM Session WHERE SessionId = :sessionID');
  $stmt->bindParam(':sessionID',$sessionID);
  $stmt->execute();
  $session = $stmt->fetch();

I have tried various things such as putting single quotes around the ':sessionID' and that prevents the syntax error but leads to nothing being taken from the database. I have ran many of these queries in the database on PHPMyAdmin and they run perfectly fine, I even have tried the generated PHP code from PHPMyAdmin but that produced syntax errors as well. I also tried PDO::PARAM_STR and that didn't make any changes. I'm not sure what else to try at this point, I'm running PHP 7.0.28 if that helps. The error is '* FROM Session WHERE SessionId='5ac81e5ab16089.68567115''

lramos15
  • 471
  • 2
  • 4
  • 8
  • add `$stmt->debugDumpParams();` after `$stmt->execute();`. What does it print? – user4035 Apr 07 '18 at 01:40
  • It outputs this. `SQL: [50] SELECT * FROM Session WHERE SessionId = :sessionID Params: 1 Key: Name: [10] :sessionID paramno=-1 name=[10] ":sessionID" is_param=1 param_type=2 ` – lramos15 Apr 07 '18 at 01:44
  • After you execute() call `$arr = $stmt->errorInfo();print_r($arr);`. What does it print? – user4035 Apr 07 '18 at 01:56
  • `Array ( [0] => 00000 [1] => [2] => )` – lramos15 Apr 07 '18 at 01:59
  • Does it mean that there are no errors? `var_dump($session);` What's inside? – user4035 Apr 07 '18 at 02:01
  • Well there is the `Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: ` but when I `var_dump($session);` there is stuff in it, which surprised me because when I add single quotes around the parameter there is no PDOException but $session contains false. This is what session has `array(6) { ["SessionId"]=> string(23) "5ac825f52b8a12.42010038" [0]=> string(23) "5ac825f52b8a12.42010038" ["UserID"]=> string(1) "2" [1]=> string(1) "2" ["Expiration"]=> string(19) "2018-04-07 21:59:17" [2]=> string(19) "2018-04-07 21:59:17" } ` – lramos15 Apr 07 '18 at 02:07
  • If you put the query into `try{}catch{}` block and output the exception message: what does it print? [PDO Exception Questions - How to Catch Them](https://stackoverflow.com/questions/11102644/pdo-exception-questions-how-to-catch-them) – user4035 Apr 07 '18 at 02:10
  • I think, it's better to put `PDO::FETCH_ASSOC` into fetch to remove duplication. – user4035 Apr 07 '18 at 02:12
  • I added `PDO::FETCH_ASSOC` and attempted to try and catch the error, but I'm still getting the uncaught exception, I followed the stackoverflow post provided just to make sure. – lramos15 Apr 07 '18 at 02:25
  • My goal is to find the cause of the exception. I'll be in the chat. – user4035 Apr 07 '18 at 02:32
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/168434/discussion-between-user4035-and-lramos15). – user4035 Apr 07 '18 at 02:32

0 Answers0