php - Not sure if im understanding OOP and PDO. Trying to learn how to use it -
im not 100% percent sure if im doing right, can @ code , fill me in on i'm doing wrong.
when test code nil happens in database, , no errors listed on page.
this config.php file
<?php // defines database connection info define('db_host', 'localhost'); define('db_user', '*******'); define('db_password', '********'); define('db_database', 'bettergamerzunited'); ?>
register.class.php
class register { private $conn; public function __construct() { seek { $this->conn = new pdo("mysql:host=".db_host.";dbname=".db_database, db_user, db_password); } catch(exception $e) { var_dump($e); } } public function __destruct() { $this->conn->close(); } public function adduser($first_name, $last_name, $username, $password, $email, $country, $gender, $ip) { seek { $query = "insert users (username, email, firstname, lastname, password, gender, country, ip, signup, lastlogin, notescheck) values (?, ?, ?, ?, ?, ?, ?, ?, now(), now(), now())"; $stmt = $this->conn->prepare($query); $stmt->bindvalue(1, $username); $stmt->bindvalue(2, $email); $stmt->bindvalue(3, $first_name); $stmt->bindvalue(4, $last_name); $stmt->bindvalue(5, $password); $stmt->bindvalue(6, $gender); $stmt->bindvalue(7, $country); $stmt->bindvalue(8, $ip); $stmt->execute(); $this->conn->close(); }catch(pdoexception $e) { echo 'there error'; } } }
then here register.php
<?php require_once 'class/register.class.php'; $register = new register(); $register->adduser('andrew', 'mccoms', 'test', 'test', 'testtest@test.com', 'usa', 'm', '192.168.0.1'); echo $register->adduser(); ?>
nothing echoing screen, , not going in database.
issue #1
you getting mixed local variables , class properties. in constructor, set $conn = $this->mmysqli = new pdo()
:
$conn = $this->mmysqli = new pdo("mysqli:host=".$db_host.";dbname=".$db_database, $db_user, $db_password);
and in ::adduser()
method, seek access $conn
variable. variable locally available in constructor method, , should go on utilize class's mmysqli
property:
public function adduser($first_name, $last_name, $username, $password, $email, $country, $gender, $ip) { seek { $this->mmysqli->setattribute(pdo::attr_errmode, pdo::errmode_exception); $this->mmysqli->prepare("insert users (username, email, firstname, lastname, password, gender, country, ip, signup, lastlogin, notescheck) values (?, ?, ?, ?, ?, ?, ?, now(), now(), now())"); $this->mmysqli->bindvalue(1, $username); $this->mmysqli->bindvalue(2, $email); $this->mmysqli->bindvalue(3, $first_name); $this->mmysqli->bindvalue(4, $last_name); $this->mmysqli->bindvalue(5, $password); $this->mmysqli->bindvalue(6, $gender); $this->mmysqli->bindvalue(7, $country); $this->mmysqli->bindvalue(8, $ip); $this->mmysqli->execute(); $this->mmysqli->close(); }catch(pdoexception $e) { homecoming 'there error'; } }
issue #2 you accessing defined constants (db_host
, etc) wrong. not need $
normal variable:
$this->mmysqli = new pdo("mysqli:host=".db_host.";dbname=".db_database, db_user, db_password);
issue #3 your pdo
constructor should utilize mysql:host
instead of mysqli:host
. mysqli
php extension access mysql, database still mysql (for accessing via pdo).
credit @fred-ii-
extrasfunction __construct() {}
should public function __construct() {}
$this->mmysqli->setattribute()
modifies entire pdo
connection, should called in constructor method. return 'there error';
won't print on page unless echo $register->adduser();
. check out how return
works. php mysql oop pdo
No comments:
Post a Comment