AndreaJana Posted August 11, 2015 Share Posted August 11, 2015 Hello there,I am newbie here and seems this is good place.I am working on registration form but have error on 1 linecod:Fatal error: Call to a member function prepare() on a non-object in C:\wamp\www\ooploginreg\functions.php on line 13Call Stack# Time Memory Function Location1 0.0010 252552 {main}( ) ..\register.php:02 0.0030 265480 LoginRegistration->registerUser( ) ..\register.php:50functions.php PHP Code: <?phprequire "config.php";class LoginRegistration{ function __construct() { $database = new DatabaseConnection(); } public function registerUser($username,$password,$name,$email,$website){ global $pdo; $query = $pdo->prepare("SELECT id FROM users WHERE username = ? AND email = ?"); $query->execute(array($username, $email)); $num = $query->rowCount(); if($num == 0){ $query = $pdo->prepare("INSERT INTO users (username, password, name, email, website)VALUES (?, ?, ?, ? ,?)"); $query->execute(array($username, $password, $name, $email, $website)); return true; }else{ return print "<span style='color:=#e53d37'>Error...username/email already used.</span>"; } }}?> config.php hope is ok: <?php class DatabaseConnection{ public function _construct(){ global $pdo; try{ $pdo = new PDO('mysql:host=localhost;dbname=oopreg', 'root', ''); } catch (PDOException $e){ exit('Database error'); } }}?> What do you thik about this error? Link to comment https://forums.phpfreaks.com/topic/297740-registration-cod-error/ Share on other sites More sharing options...
scootstah Posted August 11, 2015 Share Posted August 11, 2015 It means that $pdo is not an object in the scope that you're trying to use it. Do not use globals. Instantiate the PDO object and then pass it via a constructor argument to each class that needs it. class LoginRegistration { private $pdo; public function __construct(PDO $pdo) { $this->pdo = $pdo; } } ................. try{ $pdo = new PDO('mysql:host=localhost;dbname=oopreg', 'root', ''); } catch (PDOException $e){ exit('Database error'); } $loginRegistration = new LoginRegistration($pdo); Link to comment https://forums.phpfreaks.com/topic/297740-registration-cod-error/#findComment-1518539 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.