Jump to content

PDO ERROR


Recommended Posts

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

Link to comment
Share on other sites

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 by dkub
Link to comment
Share on other sites

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" 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.