5 Replies - 593 Views - Last Post: 14 April 2016 - 11:52 PM Rate Topic: -----

#1 dak45309   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 160
  • Joined: 22-April 13

Prepared Statement Not Returning Results

Posted 14 April 2016 - 03:04 PM

I have tested in my database this SQL query (which is prepared in my PHP code):
SELECT * FROM `users` WHERE `Username` = ? AND `Password` = ?

Simple login query here. I then bind the parameters to this query in PDO as so:
$query -> bindParam(1, $username);
		$query -> bindParam(2, $password);

Then I call:
$result = $query -> execute();

If I do:
echo $result;

That results with an output of one but if I call a column like:
echo $result["UserID"];

It ends up displaying no results.
I have looked at the password hash that was entered compared to the one the user signed up for and they matched. I tested the password it gave me when echoing it to the screen (just copied and pasted it into the SQL query in phpMyAdmin) along with the username and it brought up the one user in the database. Anything I'm not seeing here?

This post has been edited by dak45309: 14 April 2016 - 03:35 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Prepared Statement Not Returning Results

#2 astonecipher   User is offline

  • Enterprise Software Architect
  • member icon

Reputation: 3151
  • View blog
  • Posts: 11,956
  • Joined: 03-December 12

Re: Prepared Statement Not Returning Results

Posted 14 April 2016 - 03:29 PM

Quote

It ends up displaying anything.


Care to elaborate?

Using question mark placeholders, you can do this:

$result = $query -> execute( [ $username, $password] );


Then, you can't echo $result, it holds multiple values:
echo "<pre>";
print_r( $result );
echo "</pre>";

Was This Post Helpful? 0
  • +
  • -

#3 dak45309   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 160
  • Joined: 22-April 13

Re: Prepared Statement Not Returning Results

Posted 14 April 2016 - 03:43 PM

I caught it said that after re-reading. I meant to put it displays nothing. Like, it outputs a "" or NULL value. I have tried the
$query -> execute([$username, $password]);

It still outputs a "" or NULL value. I'll attach a screenshot of it.

ScreenshotAttached Image

This post has been edited by Dormilich: 14 April 2016 - 11:50 PM

Was This Post Helpful? 0
  • +
  • -

#4 astonecipher   User is offline

  • Enterprise Software Architect
  • member icon

Reputation: 3151
  • View blog
  • Posts: 11,956
  • Joined: 03-December 12

Re: Prepared Statement Not Returning Results

Posted 14 April 2016 - 03:47 PM

You're not fetching the data. The execute just executes the command. It should be,

$query->execute([$username, $password]);
$result = $query->fetch();


Not,

$result = $query->execute([$username, $password]);

Was This Post Helpful? 1
  • +
  • -

#5 dak45309   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 160
  • Joined: 22-April 13

Re: Prepared Statement Not Returning Results

Posted 14 April 2016 - 03:56 PM

Thank you! That was it! Wasn't sure what I was missing!

This post has been edited by Dormilich: 14 April 2016 - 11:51 PM

Was This Post Helpful? 0
  • +
  • -

#6 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4278
  • View blog
  • Posts: 13,573
  • Joined: 08-June 10

Re: Prepared Statement Not Returning Results

Posted 14 April 2016 - 11:52 PM

View Postdak45309, on 14 April 2016 - 11:56 PM, said:

Wasn't sure what I was missing!

the Manual (http://php.net/pdo). it describes all functions, which parameters they accept and what they return.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1