I've always included a small php file with the connection and then written classes that make a query and handle the return data.
This time, I am trying a different approach.
I created a class for the database connection and am using EXTEND for subsequent classes.
For example, I have:
Code:
class dao{
function connect_db(){
//connector function
}
function query($sql){
$this->query = mysql_query($sql);
return $this->query;
}
}
class website extends dao{
function getUsers(){
//get user function
}
}
My question is how do I run the dao->query function from within the getUsers function?
Would it be something like:
Code:
function getUsers(){
$result = parent::query("select * from tbl_user");
while($r = mysql_fetch_assoc($result)){
echo $r["user_name"].'<br />';
}
}
or can I just call it as if it were in the same class:
Code:
function getUsers(){
$result =$this->query("select * from tbl_user");
while($r = mysql_fetch_assoc($result)){
echo $r["user_name"].'<br />';
}
}
Still trying to get a hang of classes and such. Thanks for you patience and help.
Hi Guys,
...or can I just call it as if it were in the same class...
Yes (as long as you did not declare the method to be "private" in the parent class).
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
PS: Note that the MySQLi and PDO database extensions are already object-oriented, so you could simply(?) use them instead of creating your own OO implementation of the old MySQL extension.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
ps: Note that the mysqli and pdo database extensions are already object-oriented, so you could simply(?) use them instead of creating your own oo implementation of the old mysql extension.
you mean i've been reinventing fire and the wheel???
:d
thanks a million. I don't know how i missed this....[turns red]
you mean i've been reinventing fire and the wheel???
:d
thanks a million. I don't know how i missed this....[turns red]
Probably because the MySQLi extension came out with PHP5, but there is an awful lot of legacy PHP4- code out there using the older MySQL extension, and even new code that feels a need to be PHP4-compatible (even though it's been close to a year now since PHP4 has ceased being supported in any way).
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
NogDog, this has worked great. Thanks for your help.
But is it possible to write classes that extend this mysqli class?
Yes, for instance:
PHP Code:
class Foo extends mysqli
{
public function __construct($host, $user, $password, $db)
{
parent::__construct($host, $user, $password, $db);
}
public function bar()
{
echo "This is my new method added to the mysqli class";
}
}
But it all depends on what you actually need to do: perhaps it makes more sense to pass a MySQLi object to another class, for instance.
PHP Code:
class Foo
{
private $db;
public function __construct(MySQLi $db)
{
$this->db = $db;
}
public function bar($id)
{
$stmt = $this->db->prepare("SELECT * FROM `table` WHERE `id` = ?");
$stmt->bind_param('i', $id);
// and so forth...
}
}
$db = new MySQLi('localhost', 'user', 'password', 'database');
$foo = new Foo($db);
$foo->bar();
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Bookmarks