Skip to main content
improved formatting
Link
palacsint
  • 30.4k
  • 9
  • 82
  • 157

OOP MySQL Connection Classconnection class

Source Link
Wasabi
  • 195
  • 4

OOP MySQL Connection Class

Under sage from another CR post I'm crafting an OOP MySQL connection/query class. Nothing new, but a place to start plying OOP PHP. Here is the current design,what would aid speed and portability?

conf/config.php

<?php

//Inlcude the Data Access Layer: Db
require_once (dirname(dirname(__FILE__)) . '/inc/class/dbc.php');


//DB Constants...
define('DB_HOST','dbhost');
define('DB_USER','user');
define('DB_PASS','pass');
define('DB_NAME','dbname');


?>

inc/class/dbc.php

<?php  

/**
* 
*/
class Db            
{
    public $mysql;
    
    function __construct()
    {
        $this->mysql = new mysqli (DB_HOST, DB_USER, DB_PASS, DB_NAME) OR die ('Could not connect to MySQL: ' . mysqli_connect_error() );

//adding custom error checking:
            
    }// End Constructor
    
    
}//End Class Definition

index.php

<div id="todo">
<?php
                
                require_once(dirname(__FILE__) . '/conf/config.php');
                $db = new Db();
                $query = "SELECT * FROM todo ORDER BY id asc";
                $results = $db->mysql->query($query);
                
                if($results->num_rows) {
                    while($row = $results->fetch_object()) {
                        $title = $row->title;
                        $description = $row->description;
                        $id = $row->id;
                        
    ?>          

</div>