vic vance Posted March 16, 2012 Share Posted March 16, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/259055-pdo-for-sql-statements/ Share on other sites More sharing options...
kicken Posted March 16, 2012 Share Posted March 16, 2012 [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']; Quote Link to comment https://forums.phpfreaks.com/topic/259055-pdo-for-sql-statements/#findComment-1328109 Share on other sites More sharing options...
vic vance Posted March 16, 2012 Author Share Posted March 16, 2012 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.. 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. Quote Link to comment https://forums.phpfreaks.com/topic/259055-pdo-for-sql-statements/#findComment-1328194 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.