pdo, mysqli oop database classes to use with php

Started on the old forum in April 2016 · view the original in the archive · old links redirect here

4 replies · 324 views · started 10 years ago · 2 min read

3 participants
Ghost Senior · 366 posts #1

so i created 2 database classes to use with mysql/php. 1 x mysqli oop db class 1 x pdo oop db class

the aim/point of this was to build some kind of structure to using both mysqli or pdo in a slightly more clean/tidy way.

examples:

mysqli oop db class usage examples:

require_once('mysqli.class.php');
// multi row table
$categories = db::mysqli()->query('SELECT * FROM `categories`');
if($categories->count() > 0):
    foreach($categories->result() as $category):
        echo $category->title, '<br/>';
        echo $category->description, '<br/>';
    endforeach;
endif;

db::mysqli()->query('UPDATE `config` SET `site_name`="codemafia"');

// single row table
$query = db::mysqli()->query('SELECT * FROM `config`');
// then use our result function as an array instead:
$config = $query->result()[0];
echo $config->site_name, '<br/>';
echo $config->timezone, '<br/>';

db::mysqli()->close();

pdo oop db class usage examples:

require_once('pdo.class.php');
// multi row table
db::pdo()->query('SELECT * FROM `categories`');
// or with bind
//db::pdo()->query('SELECT * FROM `categories` WHERE `id` = :id');
//$placeholders = array(':id' => '1');
//db::pdo()->bind($placeholders);
db::pdo()->execute();
if(db::pdo()->count() > 0):
    foreach(db::pdo()->result() as $category):
        echo $category->title, '<br/>';
        echo $category->description, '<br/>';
    endforeach;
endif;

db::pdo()->query('UPDATE `config` SET `site_name`="codemafia"');
db::pdo()->execute();

// single row table
db::pdo()->query('SELECT * FROM `config`');
db::pdo()->execute();
// then use our result function as an array instead:
$config = db::pdo()->result()[0];
echo $config->site_name, '<br/>';
echo $config->timezone, '<br/>';

db::pdo()->close();

the files/classes themselves will be stored/hosted on GitHub due to me maybe updating from time to time/ you can get the classes yourself here at: https://github.com/x-G-h-o-s-t-x/databases

if anyone likes or uses these. a like/thanks would be appreciated.

hope you like them.

p:s i will be getting to work shortly on a new pdo forum type script. (ive dropped my mysqli version due to loony talking my ears off about pdo lol) i will be hosting/storing the progress of this on GitHub also if anyone will be interested in that also.

<?php include ('Ghost'); if ($Post == true) { echo ' ![](http://destrukt.dk/ico/phpdeveloper.jpg) alt='coding-talk.com!!' />'; echo 'Sharing Is Caring!'; } else { echo ' ![](http://img98.imageshack.us/img98/6476/av3ro4.jpg) alt='the username GHOST has been comprimised!' />'; echo 'OMG SOMEBODY HELP ME!!'; } ?>
Ghost Senior · 366 posts #3

thanks bud. glad you like it

<?php include ('Ghost'); if ($Post == true) { echo ' ![](http://destrukt.dk/ico/phpdeveloper.jpg) alt='coding-talk.com!!' />'; echo 'Sharing Is Caring!'; } else { echo ' ![](http://img98.imageshack.us/img98/6476/av3ro4.jpg) alt='the username GHOST has been comprimised!' />'; echo 'OMG SOMEBODY HELP ME!!'; } ?>
Ghost Senior · 366 posts #5

indeed loony lol. but at least you like it. it means i did something right at least lol.

<?php include ('Ghost'); if ($Post == true) { echo ' ![](http://destrukt.dk/ico/phpdeveloper.jpg) alt='coding-talk.com!!' />'; echo 'Sharing Is Caring!'; } else { echo ' ![](http://img98.imageshack.us/img98/6476/av3ro4.jpg) alt='the username GHOST has been comprimised!' />'; echo 'OMG SOMEBODY HELP ME!!'; } ?>

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close