I have a database class ... any improvements needed ??
Posted: Sun Mar 16, 2003 1:23 am
Hi I just created (largely copied and modified actually) a class for database usage.
There are 2 goals I have for it :
1. sipmle interface
2. just a VERY SIMPLE and basic implementation. I don't need anything complicated
But if i missed anything obvious could you let me know
also .. any error checks that i missed would be helpful
cheers
oh and one more thing :
Is this the best way to safetly execute a query (uses mysql_escape_string):
cheers
There are 2 goals I have for it :
1. sipmle interface
2. just a VERY SIMPLE and basic implementation. I don't need anything complicated
But if i missed anything obvious could you let me know
also .. any error checks that i missed would be helpful
cheers
Code: Select all
/****************************************************************
* Description: PHP Class for MySQL Connection
*
* EXAMPLES:
*
* SELECT
* include "class.mysql.php";
* $mysql = New mysql('localhost','database','root','password');
* if (! $mysql->connect()) echo $mysql->getError();
* if (! $mysql->query("SELECT field FROM table")) echo $mysql->getError();
* if ($mysql->num_rows() > 0) {
* while ($mysql->movenext()) {
* echo $mysql->getfield("field");
* }
* }
*
* INSERT
* include "class.mysql.php";
* $mysql = New mysql('localhost','database','root','password');
* if (! $mysql->connect()) echo $mysql->getError();
* if (! $mysql->query("INSERT INTO table (field) values ('value')")) echo $mysql->getError();
*
* UPDATE
* include "class.mysql.php";
* $mysql = New mysql('localhost','database','root','password');
* if (! $mysql->connect()) echo $mysql->getError();
* if (! $mysql->query("UPDATE table SET field='newvalue' WHERE fieldID=1")) echo $mysql->getError();
*
* DELETE
* include "class.mysql.php";
* $mysql = New mysql('localhost','database','root','password');
* if (! $mysql->connect()) echo $mysql->getError();
* if (! $mysql->query("DELETE FROM table WHERE fieldID=1")) echo $mysql->getError();
*
*
***************************************************************/
class mysql {
/* public: connection parameters */
var $host;
var $db;
var $user;
var $password;
var $error;
/* private: connection parameters */
var $conn;
var $result;
var $record;
function mysql ($host,$db,$user,$password) {
$this->host = $host;
$this->db = $db;
$this->user = $user;
$this->password = $password;
$this->error = "";
}
function connect () {
if (! $this->conn = mysql_pconnect($this->host,$this->user,$this->password)) {
$this->error = "Connection to $server failed : ".mysql_error()."<br>\n";
return false;
}
if (! mysql_select_db($this->db,$this->conn)) {
$this->error = "Error:" . mysql_errno() . " : " . mysql_error() . "<br>\n";
return false;
}
return true;
}
function query($sql) {
if (! $this->result = mysql_query($sql, $this->conn)) {
$this->error = "Error:" . mysql_errno() . " : " . mysql_error() . "<br>\n";
return false;
}
return true;
}
function num_rows() {
return mysql_num_rows($this->result);
}
function movenext(){
$this->record = mysql_fetch_array($this->rstemp);
return (is_array($this->record));
}
function getfield($field){
return($this->record[$field]);
}
function getError(){
return $this->error;
}
function close(){
mysql_close($this->conn);
}
}oh and one more thing :
Is this the best way to safetly execute a query (uses mysql_escape_string):
Code: Select all
result = mysql_query(mysql_escape_string($sql), $this->conn)