Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,471
There are 1358 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     PHP and MySQL :

mySQL database class (opinions/suggestions)

Thread title: mySQL database class (opinions/suggestions)
Closed Thread    
    Thread tools Search this thread Display Modes  
03-08-2005, 04:35 AM
#1
mike.fro is offline mike.fro
mike.fro's Avatar
Status: I love this place
Join date: Mar 2005
Location: Burnsville
Expertise: Design
Software:
 
Posts: 674
iTrader: 5 / 100%
 

mike.fro is on a distinguished road

  Old  mySQL database class (opinions/suggestions)

Hey Everyone,
I'm in the process of creating a nice mySQL database class and am interested in your opinions on it so far. Please let me know what you think and what else you feel should be added/fixed. This was coded in PHP5.

mysql.class.php:
PHP Code:
<?php

/* mySQL Database Class 
   Mike Froseth
   Testing Purposes Only (for now!)
*/

class mysql {
    
    private 
$linkid;     // mySQL link identifier
    
private $host;       // mySQL server host
    
private $user;       // mySQL user
    
private $pswd;       // mySQL password
    
private $db;         // mySQL database
    
private $result;     // mySQL result
    
private $querycount// Total queries executed
    
    /* Class constructor. Initializes the $host, $user, $pswd
       and $db fields. */
    
    
function __constuct($host$user$pswd$db) {
        
$this->host $host;
        
$this->user $user;
        
$this->pswd $pswd;
        
$this->db $db;
        
    }
    
    
/* Connects to the mySQL database server */
    
function connect() {
        try {
            
$this->linkid = @mysql_connect($this->host,$this->user,$this->pswd);
            if(! 
$this->linkid)
              throw new 
Exception("Could not connect to the mySQL server.");
        }
        catch (
Exception $e) {
            die(
$e->getMessage());
            
        }
    }
    
    
/* Selects the mySQL database. */
    
function select() {
        try {
            if (! @
mysql_select_db($this->db$this->linkid))
               throw new 
Exception("Could not connect to the mySQL database.");
               
        }
        catch (
Exception $e) {
            die(
$e->getMessage());
            
        }
    }
    
    
/* Execute database query */
    
function query($query) {
        try {
            
$this->result = @mysql_query($query,$this->linkid);
            if (! 
$this->result)
               throw new 
Exception("The database query failed.");
               
        }
        catch (
Exception $e) {
            echo(
$e->getMessage());
            
        }
        
$this->querycount++;
        return 
$this->result;
        
    }
    
/* Determine total rows affected by query. */
    
function affectedRows() {
        
$count = @mysql_num_rows($this->linkid);
        return 
$count;
        
    }
    
    
/* Determine total rows returned by query. */
    
function numRows() {
        
$count = @mysql_num_rows($this->result);
        return 
$count;
        
    }
    
    
/* Return query result row as an object. */
    
function fetchObject() {
        
$row = @mysql_fetch_object($this->result);
        return 
$row;
        
    }
    
    
/* Return query result as an indexed array. */
    
function fetchRow() {
        
$row = @mysql_fetch_row($this->result);
        return 
$row;
    }
    
    
/* Return query result row as an associative array. */
    
function fetchArray() {
        
$row = @mysql_fetch_array($this->result);
        return 
$row;
    }
    
    
/* Return total number queries executed during lifetime of this object.
       Not required, but interesting nonetheless. */
    
function numQueries() {
        return 
$this->querycount;
    }
}
?>
Thats it so far. Please let me know what you think and any suggestions you might have.

Regards,

03-09-2005, 04:12 AM
#2
patrickPaul is offline patrickPaul
Status:
Join date: Dec 2004
Location: California, US
Expertise:
Software:
 
Posts: 406
iTrader: 1 / 100%
 

patrickPaul is on a distinguished road

Send a message via AIM to patrickPaul Send a message via MSN to patrickPaul Send a message via Yahoo to patrickPaul

  Old

Not sure if the world is ready for PHP5

However I would suggest a few more class functions such as :

mysql_insert_id();
mysql_fetch_assoc() ; (fetch_array returns both associative and numbered index)
mysql_free_result(); a must!
resetNumQueries(); reset your query count, very useful for debugging purposes
saving the queries (so you can use something like fetchLastQuery()

Just a few suggestions at this point in time

Regards,
Patrick

03-09-2005, 04:13 AM
#3
mike.fro is offline mike.fro
mike.fro's Avatar
Status: I love this place
Join date: Mar 2005
Location: Burnsville
Expertise: Design
Software:
 
Posts: 674
iTrader: 5 / 100%
 

mike.fro is on a distinguished road

  Old

Originally Posted by patrickPaul
Not sure if the world is ready for PHP5

However I would suggest a few more class functions such as :

mysql_insert_id();
mysql_fetch_assoc() ; (fetch_array returns both associative and numbered index)
mysql_free_result(); a must!
resetNumQueries(); reset your query count, very useful for debugging purposes
saving the queries (so you can use something like fetchLastQuery()

Just a few suggestions at this point in time

Regards,
Patrick
Thanks a lot for the tips. I plan on creating many more functions, this was just a start for now. I'll post an update as soon as I get one!

03-09-2005, 04:17 PM
#4
Lord Kalthorn is offline Lord Kalthorn
Lord Kalthorn's Avatar
Status: I love this place
Join date: Jan 2005
Location: Greathanc
Expertise:
Software:
 
Posts: 743
iTrader: 0 / 0%
 

Lord Kalthorn is on a distinguished road

Send a message via MSN to Lord Kalthorn

  Old

Looks very confusing Rather you lot than I.

03-09-2005, 05:31 PM
#5
Adam is offline Adam
Adam's Avatar
Status: Member
Join date: Jan 2005
Location:
Expertise:
Software:
 
Posts: 433
iTrader: 0 / 0%
 

Adam is on a distinguished road

  Old

looks good, havent dealt with classes much much though

03-09-2005, 07:22 PM
#6
FiveInteractive is offline FiveInteractive
Status: Request a custom title
Join date: Jan 2005
Location: UK
Expertise:
Software:
 
Posts: 1,216
iTrader: 0 / 0%
 

FiveInteractive is on a distinguished road

Send a message via AIM to FiveInteractive Send a message via MSN to FiveInteractive

  Old

Good luck with it, actually looks pretty confusing but good luck

Closed Thread    


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed