php - Keep getting Fatal error: Call to undefined method DB::prepare() -



php - Keep getting Fatal error: Call to undefined method DB::prepare() -

that's response reflection::export(new reflectionclass($this));

class [ class users extends crud ] { @@ c:\xampp\htdocs\classes\users.php 5-76 - constants [0] { } - static properties [0] { } - static methods [0] { } - properties [7] { property [ protected $table ] property [ private $username ] property [ private $email ] property [ private $password ] property [ private $passwordrepeat ] property [ private $nickname ] property [ private $permission ] } - methods [20] { method [ public method setusername ] { @@ c:\xampp\htdocs\classes\users.php 15 - 17 - parameters [1] { parameter #0 [ $username ] } } method [ public method setemail ] { @@ c:\xampp\htdocs\classes\users.php 19 - 21 - parameters [1] { parameter #0 [ $email ] } } method [ public method setpassword ] { @@ c:\xampp\htdocs\classes\users.php 23 - 28 - parameters [2] { parameter #0 [ $password ] parameter #1 [ $passwordrepeat ] } } method [ public method setnickname ] { @@ c:\xampp\htdocs\classes\users.php 30 - 32 - parameters [1] { parameter #0 [ $nickname ] } } method [ public method setpermission ] { @@ c:\xampp\htdocs\classes\users.php 34 - 36 - parameters [1] { parameter #0 [ $permission ] } } method [ public method checkusername ] { @@ c:\xampp\htdocs\classes\users.php 38 - 49 - parameters [2] { parameter #0 [ $username ] parameter #1 [ $email ] } } method [ public method insert ] { @@ c:\xampp\htdocs\classes\users.php 51 - 64 } method [ public method update ] { @@ c:\xampp\htdocs\classes\users.php 66 - 75 - parameters [1] { parameter #0 [ $id ] } } method [ public method find ] { @@ c:\xampp\htdocs\classes\crud.php 12 - 18 - parameters [1] { parameter #0 [ $id ] } } method [ public method findall ] { @@ c:\xampp\htdocs\classes\crud.php 20 - 26 } method [ public method delete ] { @@ c:\xampp\htdocs\classes\crud.php 28 - 33 - parameters [1] { parameter #0 [ $id ] } } /** * create new db object specified database type don't * connect database * * @param string $type database type (eg "mysql") * @param array $options associative array of alternative names , values * * @return object new db object. db_error object on failure. * * @see db_common::setoption() */ method [ public method &factory ] { @@ c:\xampp\php\pear\db.php 447 - 480 - parameters [2] { parameter #0 [ $type ] parameter #1 [ $options = false ] } } /** * create new db object including connection specified database * * illustration 1. * * require_once 'db.php'; * * $dsn = 'pgsql://user:password@host/database'; * $options = array( * 'debug' => 2, * 'portability' => db_portability_all, * ); * * $db =& db::connect($dsn, $options); * if (pear::iserror($db)) { * die($db->getmessage()); * } * * * @param mixed $dsn string "data source name" or array in * format returned db::parsedsn() * @param array $options associative array of alternative names , values * * @return object new db object. db_error object on failure. * * @uses db_dbase::connect(), db_fbsql::connect(), db_ibase::connect(), * db_ifx::connect(), db_msql::connect(), db_mssql::connect(), * db_mysql::connect(), db_mysqli::connect(), db_oci8::connect(), * db_odbc::connect(), db_pgsql::connect(), db_sqlite::connect(), * db_sybase::connect() * * @uses db::parsedsn(), db_common::setoption(), pear::iserror() */ method [ public method &connect ] { @@ c:\xampp\php\pear\db.php 518 - 567 - parameters [2] { parameter #0 [ $dsn ] parameter #1 [ $options = array ] } } /** * homecoming db api version * * @return string db api version number */ method [ public method apiversion ] { @@ c:\xampp\php\pear\db.php 577 - 580 } /** * determines if variable db_error object * * @param mixed $value variable check * * @return bool whether $value db_error object */ method [ public method iserror ] { @@ c:\xampp\php\pear\db.php 592 - 595 - parameters [1] { parameter #0 [ $value ] } } /** * determines if value db_ object * * @param mixed $value value test * * @return bool whether $value db_ object */ method [ public method isconnection ] { @@ c:\xampp\php\pear\db.php 607 - 612 - parameters [1] { parameter #0 [ $value ] } } /** * tell whether query info manipulation or info definition query * * examples of info manipulation queries insert, update , delete. * examples of info definition queries create, drop, alter, grant, * revoke. * * @param string $query query * * @return boolean whether $query info manipulation query */ method [ public method ismanip ] { @@ c:\xampp\php\pear\db.php 628 - 639 - parameters [1] { parameter #0 [ $query ] } } /** * homecoming textual error message db error code * * @param integer $value db error code * * @return string error message or false if error code * not recognized */ method [ public method errormessage ] { @@ c:\xampp\php\pear\db.php 652 - 694 - parameters [1] { parameter #0 [ $value ] } } /** * parse info source name * * additional keys can added appending uri query string * end of dsn. * * format of supplied dsn in fullest form: * * phptype(dbsyntax)://username:password@protocol+hostspec/database?option=8&another=true * * * variations allowed: * * phptype://username:password@protocol+hostspec:110//usr/db_file.db?mode=0644 * phptype://username:password@hostspec/database_name * phptype://username:password@hostspec * phptype://username@hostspec * phptype://hostspec/database * phptype://hostspec * phptype(dbsyntax) * phptype * * * @param string $dsn info source name parsed * * @return array associative array next keys: * + phptype: database backend used in php (mysql, odbc etc.) * + dbsyntax: database used regards sql syntax etc. * + protocol: communication protocol utilize (tcp, unix etc.) * + hostspec: host specification (hostname[:port]) * + database: database utilize on dbms server * + username: user name login * + password: password login */ method [ public method parsedsn ] { @@ c:\xampp\php\pear\db.php 733 - 850 - parameters [1] { parameter #0 [ $dsn ] } } /** * returns given dsn in string format suitable output. * * @param array|string dsn parse , format * @param boolean true hide password, false include * @return string */ method [ public method getdsnstring ] { @@ c:\xampp\php\pear\db.php 862 - 926 - parameters [2] { parameter #0 [ $dsn ] parameter #1 [ $hidepassword ] }}}}

with reflection::export(new reflectionclass('db')); got next response:

class [ class db ] { @@ c:\xampp\htdocs\classes\db.php 5-30 - constants [0] { } - static properties [1] { property [ private static $instance ] } - static methods [2] { method [ static public method getinstance ] { @@ c:\xampp\htdocs\classes\db.php 9 - 24 } method [ static public method prepare ] { @@ c:\xampp\htdocs\classes\db.php 26 - 28 - parameters [1] { parameter #0 [ $sql ] } } } - properties [0] { } - methods [0] { } }

so, i'm new php , i'm trying larn little bit of pdo. have code , keeps showing message:

fatal error: phone call undefined method db::prepare() in c:\xampp\htdocs\classes\crud.php on line 22

here's db.php:

<?php require_once 'cfg.php'; class db{ private static $instance; public static function getinstance(){ if(!isset(self::$instance)){ seek { self::$instance = new pdo('mysql:host=' . db_host . ';dbname=' . db_name, db_user, db_pass); self::$instance->setattribute( pdo::attr_errmode, pdo::errmode_exception); self::$instance->setattribute(pdo::attr_default_fetch_mode, pdo::fetch_obj); } grab (pdoexception $e) { echo $e->getmessage(); } } homecoming self::$instance; } public static function prepare($sql){ homecoming self::getinstance()->prepare($sql); } } ?>

crud.php:

<?php require_once 'db.php'; abstract class crud extends db { protected $table; abstract public function insert(); abstract public function update($id); public function find($id) { $sql = "select * $this->table id = :id"; $qry = db::prepare($sql); $qry->bindparam(':id', $id, pdo::param_int); $qry->execute(); homecoming $qry->fetch(); } public function findall() { $sql = "select * $this->table"; $qry = db::prepare($sql); $qry->execute(); homecoming $qry->fecthall(); } public function delete($id) { $sql = "delete $this->table id = :id"; $qry = db::prepare($sql); $qry->bindparam(':id', $id, pdo::param_int); homecoming $qry->execute(); } } ?>

then when seek function code, message. example, if seek that:

<?php $crud = new users(); $crud->findall(); ?>

users.php:

<?php require_once 'crud.php'; class users extends crud { protected $table = 'bloggy_user'; private $username; private $email; private $password; private $passwordrepeat; private $nickname; private $permission; public function setusername($username) { $this->username = $username; } public function setemail($email) { $this->email = $email; } public function setpassword($password, $passwordrepeat) { if ($password == $passwordrepeat) { $cryptedpass = password_hash($password, password_bcrypt, ['cost' => 10, 'salt' => '1234567890123456789011']); $this->password = $cryptedpass; } } public function setnickname($nickname) { $this->nickname = $nickname; } public function setpermission($permission) { $this->permission = $permission; } public function checkusername($username, $email) { $sql = "select * $this->table username = :username or email = :email"; $qry = db::prepare($sql); $qry->bindparam(':username', $username); $qry->bindparam(':email', $email); $qry->execute(); if ($qry->fetchcolumn() == 0) { homecoming true; } else { homecoming false; } } public function insert() { $sql = "insert $this->table (username, email, password, nickname) values (:username, :email, :password, :nickname)"; $qry = db::prepare($sql); $qry->bindparam(':username', $this->username); $qry->bindparam(':email', $this->email); $qry->bindparam(':password', $this->password); $qry->bindparam(':nickname', $this->nickname); if ($this->checkusername($this->username, $this->email)) { homecoming $qry->execute(); } else { echo "esse usuário já existe"; homecoming false; } } public function update($id) { $sql = "update $this->table set email = :email, password = :password, nickname = :nickname, permission = :permission id = :id"; $qry = db::prepare($sql); $qry->bindparam(':email', $this->email); $qry->bindparam(':password', $this->password); $qry->bindparam(':nickname', $this->nickname); $qry->bindparam(':permission', $this->permission); $qry->bindparam(':id', $id); homecoming $qry->execute(); } } ?>

it show error message. idk if made myself clear enough, i'm noob @ pdo, if can help me i'd grateful.

when right other mistakes (see below), code works me. wonder if require_once finding different file named db.php? test adding line print "hello!"; within db.php outside class definition. run script , see if printed message appears.

if message not appear, script extending db class different file require_once finding before finds current db.php. sort of error happens time if have multiple copies of same code, , don't manage php include_path carefully.

if works, must have db class somehow doesn't have static function prepare(). can add together code script find out methods thinks has:

reflection::export(new reflectionclass('db'));

or test actual crud object before phone call prepare() in findall() method:

reflection::export(new reflectionclass($this));

okay, see output reflection export , notice this:

/** * create new db object specified database type don't * connect database * * @param string $type database type (eg "mysql") * @param array $options associative array of alternative names , values * * @return object new db object. db_error object on failure. * * @see db_common::setoption() */ method [ public method &factory ] { @@ c:\xampp\php\pear\db.php 447 - 480 - parameters [2] { parameter #0 [ $type ] parameter #1 [ $options = false ] }

notice factory() method says it's defined in c:\xampp\php\pear\db.php. , neither getinstance() nor prepare() methods of db class in reflection export @ all!

so class crud extends db finding pear db.php before finds own db class.

this why php supports namespaces, can create sure have unique identifier class, if base of operations class name conflicts other class.

or alternatively rename db class distinct mydb or dbconnection or that.

or alter include_path finds project files before pear files.

this explains why code works in environment, because i'm not using xampp or else installs pear classes.

other mistakes:

your pdo connection might fail, illustration due getting hostname or user/password wrong.

don't catch() exceptions if you're not going right root cause or homecoming failure. when connection fails in getinstance(), echo error, homecoming instance anyway.

don't phone call getinstance()->prepare() when there's chance getinstance() won't homecoming pdo object. separate 2 calls, on instance, , sec phone call prepare() only if instance returned valid pdo object.

$instance = self::getinstance(); if ($instance instanceof pdo) { homecoming $instance->prepare($sql); } else { throw new exception("not pdo"); }

you misspelled fetchall() fecthall() in findall() function.

you phone call findall() don't assign result variable.

edit: thought "$this->table" not work, turns out incorrect, work. may still need know using curly braces around variables within strings other kinds of expressions, picking array elements.

php mysql pdo

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -