Reputation: 824
Hey sorry for the stupid question. I have read lots about oop in php so i decided to try. i have a blog running procedural php and all works fine but i have mysql not mysqli on the code and i decided to upgrade to the new mysqli. the problem here is really query the database to get results. i have been on the problem for two days now. here's a code example of what i want to implement.
class myDB extends mysqli
{
//perform mysqli connection to host here and return $connection_handle
}
class siteConfig
{
private function getConfig($id)
{
$res = $connection_handle->query("SELECT * from config where id='1'");
$row = $res->fetch_assoc();
return $row['option'];
}
}
on the main index file i would do this
$c = new siteConfig();
echo $c->getConfig(1);
//and this will return the result of the query.
also please pardon my programming logic and still a very terrible newbie to the oop world
any ideas will be helpful thank you.
PS
this is working example of the code am using
$db_link = mysqli_connect(DB,HOST,DB_USER,DB,PASS,DB_NAME) or die(mysql_error());
class myDB extends mysqli
{
public function getConfig($id, $db_link) // thanks cbuckley
{
$db_link = $this->db_link;
$res = mysqli_query($this->db_link,"SELECT * from config where id='1'");
$row = mysqli_fetch_array($res);
return $row['option'];
}
}
i have defined the constants already and the connection is successful but the select is not working. on the index page this goes [code] include('inc/dbc.php');
$db = new MyDB();
echo $db->getConfig(1, $db_link);
please let me know where am missing it or if possible refactor the code for me where neccessary
Upvotes: 1
Views: 3121
Reputation: 42458
It's good to keep the separation between your database connection and any business logic, so your example is good. There are just a couple of extra things that need sorting out, namely how the application logic has access to the database handler. Here's an example:
class MyDB extends mysqli {
// any custom DB stuff here
}
class SiteConfig {
protected $db;
public function __construct(MyDB $db) {
$this->db = $db;
}
public function getConfig($id) {
$statement = $this->db->prepare('SELECT * FROM config WHERE id = ?');
$statement->bind_param('i', $id);
if ($statement->execute()) {
$row = $statement->get_result()->fetch_assoc();
return $row['option'];
}
}
}
$db = new MyDB('host', 'user', 'password', 'db');
$config = new SiteConfig($db);
var_dump($config->getConfig(1));
A couple of points of interest here:
$db
into your config class$config->db
is
declared protected, so it can't be accessed outside of the scope of the class$db
instance as a property of the SiteConfig class, so it can be used in getConfig()
$config->db
is
declared protected, so it can't be accessed outside of the scope of the classUpvotes: 2
Reputation: 13558
Why not just extend the mysqli class and add your functions to myDB?
class myDB extends mysqli
{
public function getConfig($id) // thanks cbuckley
{
$res = $this->query("SELECT * from config where id='1'");
$row = $res->fetch_assoc();
return $row['option'];
}
}
$db = new myDB;
$db->getConfig($id);
You can call any mysqli function inside myDb by using the $this keyword (which refers to itself and since "it self" extends mysqli, it will have all the functions mysqli has + the ones you add)
Upvotes: 2