Advanced MySQL Class

Discussion in 'PHP' started by -NB-, Feb 10, 2007.

  1. #1
    Check out this handy little MySQL class. Has all the needed features, even records errors and queries!
    <?php
    	class mysql {
    		var $hostname = 'localhost';
    		var $username = '';
    		var $password = '';
    		var $database = '';
    		var $queries = array();
    		var $errors = array();
    		function mysql () {
    			return (mysql_connect($this->hostname, $this->username, $this->password) && mysql_select_db($this->database)) ? true : false;
    		}
    		function query ($query, $name) {
    			$this->queries[$name] = mysql_query($query) or die(mysql_error());
    		}
    		function fetch ($method, $name) {
    			switch ($method) {
    				case 'array':
    					return mysql_fetch_array($this->queries[$name]);
    				break;
    				case 'assoc':
    					return mysql_fetch_assoc($this->queries[$name]);
    				break;
    				case 'field':
    					return mysql_fetch_field($this->queries[$name]);
    				break;
    				case 'lengths':
    					return mysql_fetch_lengths($this->queries[$name]);
    				break;
    				case 'object':
    					return mysql_fetch_object($this->queries[$name]);
    				break;
    				case 'row':
    					return mysql_fetch_row($this->queries[$name]);
    				break;
    			}
    		}
    		function countRows ($name) {
    			return mysql_num_rows($this->queries[$name]);
    		}
    		function setError ($error, $name) {
    			$this->errors[$name] = $error;
    		}
    	}
    	$mysql = new mysql;
    ?>
    PHP:
    Thanks for viewing! Comments and rep much appreciated :D
     
    -NB-, Feb 10, 2007 IP
  2. designcode

    designcode Well-Known Member

    Messages:
    738
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    118
    #2
    Quite clean and good class, but very basic. I am currently using my own coded DB class. If I got time, I will release a public version of that.
     
    designcode, Feb 11, 2007 IP
  3. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #3
    I disagree actually, theres no inheritable database object, assigning names to queries doesn't really make much sense, it's not something I would use or encourage others to use, you've taken mysql's own php functions and wrapped them you haven't made it any easier to build queries.
     
    krakjoe, Feb 11, 2007 IP
  4. -NB-

    -NB- Peon

    Messages:
    153
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #4
    How could assigning names to queries not make sense? It's just like assigning a variable, but easier. Example:

    <?php
    $mysql->query("SELECT * FROM `table`", 'randomSelect');
    ?>
    PHP:
    With that, you can do anything you want with it. You can count the rows, fetch results, etc., but without the need of a constant variable. It records queries so you can look back on it, and if there is an error, it records it as well.
     
    -NB-, Feb 11, 2007 IP
  5. designcode

    designcode Well-Known Member

    Messages:
    738
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    118
    #5
    But NB can't it be done this way?

    <?
    $queries[] = mysql_query("SELECT * FROM `table`", 'randomSelect');
    ?>
    PHP:
    But I still say, its a better class for small applications like guestbook etc.
     
    designcode, Feb 12, 2007 IP
  6. intoex

    intoex Peon

    Messages:
    414
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #6
    my suggesting
    make different methods for different types of retrieving data
    switch is a antipattern
     
    intoex, Feb 12, 2007 IP
  7. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #7
    The main reasons I use db wrapper classes are as follows:
    1. to make queries simpler/shorter/easier
    2. to auto-disinfect user input before adding it to a query
    3. to pre-format the output to some extent.
     
    exam, Feb 12, 2007 IP
  8. designcode

    designcode Well-Known Member

    Messages:
    738
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    118
    #8
    Using a db wrapper class makes it also easier to log data, purify user input etc. Also if you are shifting your DB, like you are moving to to MSSQL from Mysql, you will have to change DB class only, you don't have to go through the whole code.
     
    designcode, Feb 12, 2007 IP
  9. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #9
    With PHP6 in the works, we really should be onto PHP5 by now - I can't see any point in posting deprecated code.

    Back on mySQL classes, as already mentioned, yours is fairly useless. Personally I use one class that handles the database connection. Then for each query I create a new object of the Query class - this uses the singleton pattern to grab the database connection, executes the query and holds the resource, all in the construct method. I then can perform any php mysql function with overloading methods and asap free the result resource with the destruct method. eg
    $res = new Query('SELECT * FROM table');
    if ( $res->hasResults ) {
      while ( $data = $res->moveNext() ) {
        print_r($data);
      }
    }
    PHP:
    And using the autoload function, I don't even need to do any includes.
     
    rodney88, Feb 12, 2007 IP
  10. -NB-

    -NB- Peon

    Messages:
    153
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Mine is purely for organizational purposes :). It doesn't add much extra functionality to MySQL, except the logging of queries and errors.
     
    -NB-, Feb 15, 2007 IP