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
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.
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.
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.
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.
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.
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.
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.
Mine is purely for organizational purposes . It doesn't add much extra functionality to MySQL, except the logging of queries and errors.