Page 1 of 1

OO way of accessing mysql DB??

Posted: Tue Mar 18, 2008 7:12 am
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();
}
 

Re: OO way of accessing mysql DB??

Posted: Tue Mar 18, 2008 7:20 am
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.

Re: OO way of accessing mysql DB??

Posted: Tue Mar 18, 2008 7:23 am
by onion2k
Use ADODB Lite. It's object oriented, fast, and pretty damn amazing.

Re: OO way of accessing mysql DB??

Posted: Tue Mar 18, 2008 11:47 am
by devbro
look up ez_sql class set.

it is what wordpress uses and it is easy to implement.

Re: OO way of accessing mysql DB??

Posted: Wed Mar 19, 2008 1:51 am
by timvw
I go along with onion2k.. ADODB has prooven to be a solid library throughout the years...