Jump to content

PDO for SQL statements


vic vance

Recommended Posts

Hello, I am learning PDO SQL statements, I have mananged to connect using PDO:

 

 

try 
	{
		$this->link = $dbh = new PDO('mysql:host='.$this->dbhost.';dbname='.$this->dbname.'', $this->dbuser, $this->dbpass);
	} 
	catch (PDOException $e) 
	{
		echo 'Connection failed: ' . $e->getMessage();
	}

 

 

I am not getting any error, so I guess that is a good start. I then tried the PDO SQL statement out. My old mySQL query is commented out in there:

 

 

if (isset($_SESSION['id']) && isset($_SESSION['password']))
{
	$_SESSION['id']			= ( isset( $_SESSION['id'] ) ) ? $_SESSION['id'] : FALSE;
	$_SESSION['password']	= ( isset( $_SESSION['password'] ) ) ? $_SESSION['password'] : FALSE;


	//$logged	=	mysql_query("SELECT * FROM `db_members` WHERE `id`='".$_SESSION['id']."' AND `password` = '".$_SESSION['password']."'");
	//$logged	=	mysql_fetch_array(	$logged	);

	// the new pdo statement
	$ff = $dbh->prepare('SELECT * FROM db_members WHERE id = '.$_SESSION['id'].' AND password = '.$_SESSION['password'].'');

	$ff->execute();
	$logged = $ff->fetchAll();

	echo $logged['username'];
}

 

 

I am trying to assign the session to logged variable. So all I am asking is go into db_members and check the id and password that is the same as session id and password and collect the rows data such as username.

 

My login script and everything works perfectly, even session id and password is valid when echo'd but I cannot assign it to the logged variable like my old sql statements.

 

 

$logged	=	mysql_query("SELECT * FROM `db_members` WHERE `id`='".$_SESSION['id']."' AND `password` = '".$_SESSION['password']."'");
$logged	=	mysql_fetch_array(	$logged	);

 

 

I used other PDO statements and it works perfectly but I just don't understand why this is not working.... can I please get some help if you have any solution to this?

Link to comment
Share on other sites

[m=pdostatement.fetchall]fetchAll[/m] returns a multi-dimension array with the first dimension being a list of rows and the second being the columns.  You either need to adjust your code to take that into account (eg echo $logged[0]['username']) or change it to use [m=pdostatement.fetch]fetch[/m] which will only return one row.

 

 

Also if your going to use PDO, you should take advantage of bound parameters:

$ff = $dbh->prepare('SELECT * FROM db_members WHERE id = :id AND password = :pass ');

$ff->execute(array(':id' => $_SESSION['id'], ':pass' => $_SESSION['password']));

$logged = $ff->fetch();
echo $logged['username'];

Link to comment
Share on other sites

Firstly, thank you soo much because my head was hurting like never before. I am sure I tried that.. I don't know I am sure.. :shrug: I did realise an three hours ago the column passwords was the problem. I did not know how to solve it. However I think I get it now.. Thanks alot once again.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.