php - Call to undefined method Database::prepare() oop pdo -
i have constructor in users class:
public function __construct($pdo) { $this->pdo = $pdo; }
and that's how run it:
index.php:
include("config.php"); $users = new users($pdo);
but don't want way, wanted have separated class database connection
created database.class.php
class database { public function __construct() { try { $pdo = new pdo('mysql:host='.mysql_host.';dbname=driptone', mysql_user, mysql_password); $pdo->setattribute(pdo::attr_errmode, pdo::errmode_exception); echo 'connected'; } catch(pdoexception $e) { echo $e->getmessage(); } } }
and used this:
$users = new users(new database());
i error:
connected fatal error: call undefined method database::prepare() in c:\xampp\htdocs\drip\class\users.class.php on line 75
i tried static, same problem.
why happening? how can fix it? , secure injections/xss attacks?
/** * public method register * * registers user system, checking errors. * if error found, throw new exception. * * @parm username username user posted. * @parm password password user posted. * @parm repassword validated password user posted. * @parm email email user posted. * @parm reemail validated email user posted. * @parm day day user posted (for date of birth). * @parm month month user posted (for date of birth). * @parm year year user posted (for date of birth). * * @return return true means correct, register successfully. **/ public function register($username, $password, $repassword, $email, $reemail, $day, $month, $year) { global $pdo; // check if passwords matching. if ($password != $repassword) { throw new exception ("passwords not match."); } // check if emails matching. else if ($email != $reemail) { throw new exception ("emails not match."); } // main insert query $this->insert = $this->pdo->prepare (" insert users (user_name, user_password, user_email, user_birth) values (:username, :password, :email, :birth) "); //query check if username taken. $this->user = $this->pdo->prepare("select * users user_name = :name"); $this->user->execute(array(":name" => $username)); //query check if email taken. $this->email = $this->pdo->prepare("select * users user_email = :email"); $this->email->execute(array(":email" => $email)); // checking if username taken using query. if ($this->user->rowcount()) { throw new exception ("username in use"); } // checking if email taken using query. else if ($this->email->rowcount()) { throw new exception ("email in use"); } // checking if birth of date valid. else if ($day > 31 || $month > 12 || $year > date('y') || $year < 1925) { throw new exception ("invalid birth of date"); } // checking if password more 5 characters long. else if (strlen($password) < 5) { throw new exception ("password short"); } else { // fine, insert data. $this->insert->execute(array ( ":username" => $username, ":password" => $password, ":email" => $email, ":birth" => $day.'/'.$month.'/'.$year )); //send verification $this->sendverification($username, $email); //finished processing, return true. return true; } }
Comments
Post a Comment