Reputation: 225
so I am creating a membership website so far I only have 2 classes a class for connecting got the database and a class to manage users
here is the class for the database:
class dbConnect
{
protected $db_conn;
public $db_host = '127.0.0.1';
public $db_user = 'root';
public $db_pass = '';
public $db_name = 'db';
public 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();
}
}
}
Here is the users class it only has 1 method I just started this project
class ManageUsers
{
public $link;
function __construct()
{
$db_connection = new dbConnect();
$this->link = $db_connection->connect();
return $this->link;
}
function registerUsers($username,$email,$password,$ip_adress,$date)
{
$query = $this->link->prepare("INSERT INTO `users` (username,email,password,ip_adress,date_joined) VALUES(?,?,?,?,?)");
$values = [$username,$email,$password,$ip_adress,$date];
$query->execute($values);
$confirm = $query->rowCount();
return $confirm;
}
}
Now I'm just running a test object to see if all this s functioning
$test = new ManageUsers();
echo $test->registerUsers('bob','[email protected]','lol','127.0.0.1','2012');
now I am getting the error that I am calling the prepare statement to a non object. Which to be honest I don't understand since I creating the object in the construct method. well any advice helps Thanks!
Upvotes: 0
Views: 84
Reputation: 57
I have a really similar script from you here it is
class dbConnect
{
protected $db_conn;
public $db_host = '127.0.0.1';
public $db_user = 'root';
public $db_pass = 'roadmin';
public $db_name = 'todo';
public 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();
}
}
}
class ManageUsers
{
public $link;
function __construct()
{
$db_connection = new dbConnect();
$this->link = $db_connection->connect();
return $this->link;
}
function registerUsers($username,$email,$password,$ip_adress,$date)
{
$query = $this->link->prepare("INSERT INTO `users` (username,email, password, ip_adress, date_joined) VALUES(?,?,?,?,?)");
$values = [$username,$email,$password,$ip_adress,$date];
$query->execute($values);
$confirm = $query->rowCount();
return $confirm;
}
function loginUser()
{
}
}
$test = new ManageUsers();
echo $test->registerUsers('bob','[email protected]','lol','127.0.0.1','2012');
Upvotes: 1
Reputation: 2556
I think PDO object is invalid, because you are using double quotes with method/property of object, in that case you need use complex string notation("{$object->property}"
) or join strings with .
(a dot)
$this->db_conn = new PDO("mysql:host=$this->db_host;dbname=$this->db_name", $this->db_user,$this->db_pass);
change to:
$this->db_conn = new PDO("mysql:host={$this->db_host};dbname={$this->db_name}", $this->db_user,$this->db_pass);
Upvotes: 3