OO way of accessing mysql DB??

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
tuscanidream
Forum Newbie
Posts: 1
Joined: Tue Mar 18, 2008 6:59 am

OO way of accessing mysql DB??

Post by tuscanidream »

I was wondering what is the best and easiest way of accessing a mysql database? Possibly and object oriented approach? I am not really familiar with classes. Post your methods! Heres how I currently am accessing my db;

Code: Select all

 
function db_connect() {
    // Create new database connection -host, -username, -password, -database name
    @ $conn = new mysqli ('host', 'username', 'password', 'db');
    if (mysqli_connect_errno()) {
        echo "<p>Error: Could not connect to the database. Try again later.</p>";
        exit;
    }
    return $conn;
}
 
This is just one example (simplified for your viewing pleasure, of course) of a function I use with the db connect function above. Most of the functions are similar to the one below, with some minor changes to the loop for the rows. Thats why I am thinking an OO approach might be easier.

Code: Select all

 
function entries($tag, $page, $postNumber) {
    //Database Query
    $conn = db_connect();
    $query = "SELECT * FROM entries, users WHERE entries.userId = users.userId ORDER BY timestamp DESC LIMIT $from, $postNumber";
    $result = $conn->query($query);
 
    //Display Entries
    if ($result->num_rows > 0) {
        while ($row = $result->fetch_object()) {
            echo "Posted By ".$row->name;
        }
    }
    //If there are no entries in the database (prevent infinate loop)
    else if (!($result->num_rows > 0) && ($page == 1)) {
        echo "<dt>We're sorry...</dt>";
        echo "<dd>There are no entries in the database yet... Please check again later.</dd>";
    }
    //If that page does not exist
    else {
        entries($tag, 1, $postNumber);
    }
 
    //Close Database
    $conn->close();
}
 
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: OO way of accessing mysql DB??

Post by s.dot »

I believe if you search the forums for a database wrapper you'll find lots of object oriented solutions. Here would be a quick start on one (excuse errors, i'm free-styling. B-))

Code: Select all

 
<?php
 
class db
{
    private $link;
    public static $instance = null;
 
    public static function getInstance()
    {
         if (self::$instance == null)
         {
             self::$instance = new db();
         }
 
         return self::$instance;
    }
 
    public function connect($host, $username, $password)
    {
       if (!$this->link = mysql_connect($host, $username, $password))
       {
            //error handling here
       }
    }
 
    public function selectDb($dbName)
    {
        mysql_select_db($dbName, $this->link);
     }
 
     // continue
}
Used like this

Code: Select all

<?php
 
require_once 'db.class.php';
$db = db::getInstance();
 
$db->connect('localhost', 'username', 'password');
$db->selectDb('my_database');
The more functions you add, the more it can be broken down into separate classes to deal with different things.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: OO way of accessing mysql DB??

Post by onion2k »

Use ADODB Lite. It's object oriented, fast, and pretty damn amazing.
devbro
Forum Newbie
Posts: 7
Joined: Tue Mar 18, 2008 11:46 am

Re: OO way of accessing mysql DB??

Post by devbro »

look up ez_sql class set.

it is what wordpress uses and it is easy to implement.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Re: OO way of accessing mysql DB??

Post by timvw »

I go along with onion2k.. ADODB has prooven to be a solid library throughout the years...
Post Reply