akaghachinaka Posted May 6, 2017 Share Posted May 6, 2017 Trying to get my hands on PDO but i keep getting an error. Below are my codes. I have two files 1. The database connection class called "class.database.php" <?php class dbConnection{ protected $db_conn; public $db_name = 'db_learning'; public $db_user = 'root'; public $db_pass = ''; public $db_host = 'localhost'; function connect(){ try{ $this->db_conn = new PDO('mysql:host = $this->$db_host;dbname = $this->db_name', $this->db_user,$this->db_pass); return $this->db_conn; } catch(PDOException $e){ return $e->getMessage(); } } } ?> 2. The class where all the processing takes place called "class.ManageUsers.php" below is the code <?php include_once('class.database.php'); class ManageUsers{ public $link; function __construct(){ $db_connection = new dbConnection(); $this->link = $db_connection->connect(); return $this->link; } function registerUsers($username,$password,$ip_address, $time, $date){ $query = $this->link->prepare("INSERT INTO `users` (`username`,`password`,`ip_address`,`reg_time`,`reg_date`) VALUES('$username','$password','$ip_address','$time','$date')"); $values = array($username,$password,$ip_address,$time,$date); /*$query = $this->link->prepare("INSERT INTO `users` (`username`) VALUES('$username')"); $values = array($username);*/ $query->execute($values); $counts = $query->rowCount(); return $counts; //return var_dump($counts); } } $users = new ManageUsers(); echo $users->registerusers('bob','bob','127.494.439.39.340','12:00','29:02:2017'); ?> when i run the code i get "0" and nothing happens in the database. Please help! class.database.php class.ManageUsers.php Quote Link to comment Share on other sites More sharing options...
dkub Posted May 6, 2017 Share Posted May 6, 2017 (edited) What's the purpose of your dbConnection class? It simply wraps PDO, offering no new functionality, but having the disadvantage of hard-coded connection parameters with public visibility. You're likely better off simply using PDO directly. You're also better off inserting the dependency into ManageUsers rather than leaving it as a hidden dependency inside the constructor. $query = $this->link->prepare("INSERT INTO `users` (`username`,`password`,`ip_address`,`reg_time`,`reg_date`) VALUES('$username','$password','$ip_address','$time','$date')"); That's not how prepared statements work. http://php.net/manual/en/pdo.prepare.php A few additional things: function registerUsers($username,$password,$ip_address, $time, $date){ echo $users->registerusers('bob','bob','127.494.439.39.340','12:00','29:02:2017'); registerUsers !== registerusers Don't store passwords as plain text. Use password_hash. That's not how IP addresses are formatted. Why are you storing date and time separately? Why are you storing dates in a weird format? Edited May 6, 2017 by dkub Quote Link to comment Share on other sites More sharing options...
akaghachinaka Posted May 6, 2017 Author Share Posted May 6, 2017 @dkub thanks for your response. This actually not a project i am just trying to understand how PDO works. Quote Link to comment Share on other sites More sharing options...
akaghachinaka Posted May 6, 2017 Author Share Posted May 6, 2017 What's the purpose of your dbConnection class? It simply wraps PDO, offering no new functionality, but having the disadvantage of hard-coded connection parameters with public visibility. You're likely better off simply using PDO directly. You're also better off inserting the dependency into ManageUsers rather than leaving it as a hidden dependency inside the constructor. $query = $this->link->prepare("INSERT INTO `users` (`username`,`password`,`ip_address`,`reg_time`,`reg_date`) VALUES('$username','$password','$ip_address','$time','$date')"); That's not how prepared statements work. http://php.net/manual/en/pdo.prepare.php A few additional things: function registerUsers($username,$password,$ip_address, $time, $date){ echo $users->registerusers('bob','bob','127.494.439.39.340','12:00','29:02:2017'); registerUsers !== registerusers Don't store passwords as plain text. Use password_hash. That's not how IP addresses are formatted. Why are you storing date and time separately? Why are you storing dates in a weird format? @dkub thanks for your response. This actually not a project i am just trying to understand how PDO works. So i actually understand that i am not suppose to store passwords as plain text i also know that time and date dot have to be store seperately. However i have made the correction with regards to the "registerUsers" function i still get "0" Quote Link to comment Share on other sites More sharing options...
benanamen Posted May 6, 2017 Share Posted May 6, 2017 i also know that time and date dot have to be store seperately. You understand wrong. There a a datetime column type. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted May 6, 2017 Share Posted May 6, 2017 However i have made the correction with regards to the "registerUsers" function i still get "0" Then show your new code, so that we're actually talking about the same thing. Your overall understanding of PDO seems rather poor. I recommend you start with the basics, namely how to set the right connection parameters; you'll need to at least enable exceptions and disable the emulation of prepared statements how to handle errors; you definitely do not catch exceptions to print the error message on the screen; exceptions should usually be left alone how prepared statements work PDO is fairly smart when used correctly, so the whole do-something-and-check-if-it-worked procedure is obsolete. If exceptions are enable, the query will either succeed or throw an exception; it will not fail and just keep running like some early PHP interfaces. Quote Link to comment Share on other sites More sharing options...
akaghachinaka Posted May 8, 2017 Author Share Posted May 8, 2017 Then show your new code, so that we're actually talking about the same thing. Your overall understanding of PDO seems rather poor. I recommend you start with the basics, namely how to set the right connection parameters; you'll need to at least enable exceptions and disable the emulation of prepared statements how to handle errors; you definitely do not catch exceptions to print the error message on the screen; exceptions should usually be left alone how prepared statements work PDO is fairly smart when used correctly, so the whole do-something-and-check-if-it-worked procedure is obsolete. If exceptions are enable, the query will either succeed or throw an exception; it will not fail and just keep running like some early PHP interfaces. Thanks for your response. I will read the article, make amendments hopefully it should work this time. Quote Link to comment 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.