View Single Post
Old 11-15-2007, 12:24 AM   #3 (permalink)
dschreck
The Contributor
 
dschreck's Avatar
 
Join Date: Nov 2007
Location: California
Posts: 82
Thanks: 0
dschreck is on a distinguished road
Default

Typed this up quickly... just an example of some things i noticed that may help you.
This is in PHP4, since that's what it looked like you were using..
But I left some comments for php5 usage.
Disclaimer: I didnt actually test the code, i just typed it. but no syntax errors, and everything looks like it should work. Again, just an example.
PHP Code:
<?php

// It looks like you're using PHP4, so this example is in PHP4.
// I'll leave notes if you are using PHP5, to as what you'd want to do.
// This is just an example of what you wish you want. and what you should do.

class mysql {
    var 
$connection// PHP5 : private $connection - this is our pointer
    
var $result;     // PHP5 : protected $result - this is our attribute that will store our results. Use this for debugging.
    
var $sql;        // PHP5 : protected $sql - this will store our query, for debugging.

    
function Connect($host$name$pass$db) { // PHP5 : public function
        
$this->connection mysql_connect($host,$name,$pass); // you dont need quotes around these items
        
mysql_select_db($db$this->connection);        
        return 
true;  // have a return type, so you know it didnt fail. 

    
}//ends the connection function

    
function Close(){ // php5 : public function
        
mysql_close($this->connection); 
        return 
true;
    }
//ends the close function


    
function FetchArray($query){ // php5 : public function    
        /* I added this if / else statement, to this method and the one below
           This will allow you to either pass it directly a SQL statement, or a resource object
           Ex:
               $sql = "SELECT * FROM Table WHERE 1=1";
               $get = $DB->FetchArray($sql);
               
               -- OR --
               
               $sql = "SELECT * FORM Table WHERE 1=1";
               $get = $DB->Query($sql);
               while($r = $DB->FetchArray($get)) {
                   ...
               }
        */
        
if(is_resource($query)) {
            
$rows mysql_fetch_array($query);
        } else {
            
$rows mysql_fetch_array($this->Query($query));
        }
        return 
$rows;
    }

    function 
FetchNum($query){
        if(
is_resource($query)) {
            
$num mysql_num_rows($query);
        } else {
            
$num mysql_num_rows($this->Query($query));
        }
        return 
$num;
    }

    function 
Query($sql){  // This is again, a public method, and it will be our most basic method,
        
$this->sql $sql;         // and our most important. This sets the SQL and Result parameters.
        
$this->result mysql_query($this->sql) or die(mysql_error());
        return 
$this->result;
    }
//ends the query function

}//ends the class

/* Hope this helps a little.. just wanted to show you how you can
    expand this in a way that will allow you to expand it  as your 
    projects grow. -dschreck
*/


?>
dschreck is offline  
Reply With Quote