Prepared statements and class inheritance in PHP -
i have problem when doing prepared statement in php. main goal create 2 classes, first 1 connecting (conexion) server , database , second 1 prepared statements (realizarconsultas).
i have decided class used statements should inherit "conexion". doing pretend connect database each time use function "realizarconsultas::mostrarconsulta()". function should connect database, should prepared statement , should close connection.
class used connection.
<?php class conexion extends pdo{ protected $conexiondb; public function conexion(){ try{ $conexiondb=new pdo ('mysql:host=localhost;dbname=euroburo','root',''); $conexiondb->setattribute(pdo::attr_errmode, pdo::errmode_exception); $conexiondb->exec("set character set utf8"); }catch(exception $e){ echo "error: ".$e->getline(); } } public function stopconexion(){ $conexiondb->close(); } } ?>
class used prepare statements.
<?php require("conexion.php"); class realizarconsulta extends conexion{ public function realizarconsulta(){ parent::__construct(); } public function mostrarconsulta($consulta){ $resultado=$this->conexiondb->prepare($consulta); $resultado->execute(); $final = $resultado->fetch(pdo::fetch_assoc); print_r($final); $this->stopconexion(); } } ?>
my main problem in line, located inside "realizarconsulta::mostrarconsulta()".
$resultado=$this->conexiondb->prepare($consulta);
each time call function (realizarconsulta::mostrarconsulta());
this error appears: call member function prepare() on non-object.
i understand error tells me $this->conexiondb not object don't understand why. reason don't understand error due fact $conexiondb protected variable created in parent class, protected variable "realizarconsulta" class should inherit without problem , should have access it. $conexiondb can see initialised when constuctor called, , should hold information related connection.
as can see through reasoning, $conexiondb should reference object belongs "conexion" class , should have access in "realizarconsulta" class.
i know in failing , how can resolve error. thanks
you're declaring class level variable, unless you're leaving out code, you're not assigning local variable class level. in php, have that. example
class { protected $a; public function __construct() { $this->a = 'hello'; } public function echo() { $a = 'world'; echo $this->a . ' ' . $a; } } $a = new a(); $a->echo()
would echo hello world
. because in echo()
, $a
local variable, , $this->a
variable scoped class. they're 2 different variables hold 2 different values.
so constructor should this
public function __construct() { try { $conexiondb= new pdo('mysql:host=localhost;dbname=euroburo', 'root', ''); $conexiondb->setattribute(pdo::attr_errmode, pdo::errmode_exception); $conexiondb->exec("set character set utf8"); $this->conexiondb = $conexiondb; } catch(exception $e) { echo "error: ".$e->getline(); } }
also, side note, it's highly recommended use public function __construct()
constructor in php, not function same name class
Comments
Post a Comment