PHP MySQL Connector Class
This class is basically the object-oriented version of MySQLi written with MySQL. It doesn't have any of the benefits of MySQLi, except that you can use the object-oriented approach. Since MySQLi has been released, I would highly recommend using that instead.
The code provided doesn't have every MySQL function available, but rather only the ones I used most often, however, it's easy to add new attributes or functions.
The Code
- <?php
- // database class
- // contains methods for connecting
- // and retrieving database stuff
- class q_fns{
- public $query;
- public $num_rows;
- public function __construct($s_sql){
- }
- public function fetch_assoc(){
- }
- public function fetch_array(){
- }
- public function fetch_row(){
- }
- }
- class db_connect{
- protected $host = "localhost";
- protected $user = "username";
- protected $pass = "password";
- protected $data = "database";
- public function __construct(){
- $connect = mysql_connect($this->host, $this->user, $this->pass) or die("Couldn't connect to MySQL.\n" . mysql_error());
- $dbpick = mysql_select_db($this->data, $connect) or die("Couldn't select MySQL database.\n" . mysql_error());
- }
- public function query($sql){
- return(new q_fns($sql));
- }
- }
- /*Implementation:
- $mysql = new db_connect;
- $result = $mysql->query("select * from `table`");
- $num_rows = $result->num_rows;
- while($row = $result->fetch_assoc()){
- echo $row['field'];
- }
- */
- ?>
The Download
| Filename | database.class.php |
|---|---|
| Filesize | 1258 bytes |
| Filetype | PHP Script |
| MD5 checksum | 9821ca6a003a132d04a0047742b2eb74 |
| License | © 2007-2009 by Sculch, LLC. under the MIT License |
