To add to this, I hope you wont mind me posting my own personal database class that I use almost all the time.
PHP Code:
<?php
/*
MySQL Database Class -------------------- Author: Matthew Wilson Site: http://www.icdsoftware.com Contact: admin at icdsoftware.com License: Free for non-commercial use.
About ----- Contains MySQL query operations within an object as well as simplifying some query types.
$settings = array( 'username', // Database Username 'password', // Database Password 'host', // Database Host 'database', // Database Name 'pconnect', // Use a permanent connection ? (1 or 0) 'backquotes', // Whether or not to use backquotes in generated queries ( 1 or 0 ) 'serialiaze' // When building queries using the easy methods (insert() & update()), should arrays/objects be automatically serialized? );
// It will automatically escape values passed, so you dont have to run an escape on every value in the insert/update array. // You might want to update:
$db->update( 'table_name', $insert, 'id=5' );
// If you need to use something like "fieldname = fieldname+x", use the expr() function:
To add to this, I hope you wont mind me posting my own personal database class that I use almost all the time.
I don't even dare mind it.
I used adoDB but it seemed a bit too much for me. Since now we have the "ultimate" PHP5.1(.1) with PDO I think I'll check that out and if it's good I'll use it instead of anything I use now (with the exception of some neat functions ).
__________________
I'm not sure if this was any help, but I hope it didn't make you stupider.
Experience is something you get just after you really need it. PHP Installation Guide Feedback welcome.
Since now we have the "ultimate" PHP5.1(.1) with PDO I think I'll check that out and if it's good I'll use it instead of anything I use now (with the exception of some neat functions ).
yeah definatley, I cant wait till 5.1.1 is mainstream, but at the moment with so many servers running 4.3.0 - 4.4.1 (I think i got the version numbers right) its no good using PDO.
That is a nice db class. Mind if I use a few of the ideas (mainly the cool variable name, and the makeQuery())? Mine is internal...I doubt I'll ever post it in any way. Just curious (and probably missing something due to lack of sleep), but what purpose does the SQL_EXPRESSION serve?
The reason i have an SQL_EXPRESSION constant is to allow things like "var = var+1" as an update query. When you call the update() method, it takes an associative array. If you want to use an expression (eg: var + 1 ) you would do:
PHP Code:
$update = array(
'var' => expr( 'var+1' )
);
The expr() function creates an array of the SQL_EXPRESSION constant, and the expression you entered. When it comes to building the query, datatypes are checked and escaped appropriately. 'var+1' would be treated as a string, and not give the desired effect.
If you look into the makeQuery function, you see when the switch loop gets to processing arrays, the first thing it does is checks fort the existance of the SQL_EXPRESSION constant as the first value. If it finds it, then it puts the value into the query as an expression rather than a string, and doesn't go on to potentially serialize the array (one of the options that you can set at the start -- automatically serialize arrays)
missing-score, your class looks generally good.. one comment is to use mysql_real_escape_string. I was writing a similar abstraction for the Oracle/oci functions today although I was using objects and extending the base class then checking the class name instead of using constants like you did with expr() .. not sure if I'll convert over to your approach when I head in tomorrow but I have a feeling I will.
also, insert and update can have extremely similar syntax:
Code:
INSERT INTO table SET `col1`= 'blah';
UPDATE table SET `col1`= 'blah' WHERE `col2`='blah';
one thing that could be done (for missing-score's):
Code:
class mysql_query_resource {
var $_query;
var $_rs;
var $_db;
function mysql_query_resource($query, $db) {
$this->_query = $query;
$this->_db = $db;
$this->_rs = @mysql_query($query, $db)
$this->is_error = !($this->_rs);
if (MYSQL_DEBUG && $this->isError()) { $this->getError(); }
}
function isError() {
return($this->is_error);
}
function getError() {
/* some error handling here */
}
function getResult($ field) {
return mysql_result($rs, 0, $field);
}
function getAssoc() {
return mysql_fetch_assoc($rs);
}
/* and so on ..*/
}
class mysql {
var $_db;
/* blah blah */
function query($query) {
return new mysql_query_resource($query, $this->_db);
}
/* blah blah */
}
// so..
$db = new mysql($connect);
$rs = $db->query("SELECT * From..");
while ($row=$rs->getAssoc()) {
print_r($row);
}
// and .. you get the idea.