* Created on 2010-4-21 * * The class for control MySQL * * Made by S71ence * * @ $host * @ $user _name * @ $user _pwd * @ $data _base * @ $coding */ Class MySQL { private $host;//Host Name Private $user _name;//User name Private $user _pwd;//Password Private $data _base;//Database name private $coding;/code constructor to initialize the operation function __construct ($host, $user _name, $user _pwd, $data _base, $coding) { $this->host= $host; $this->user_name= $user _name; $this->user_pwd= $user _pwd; $this->data_base= $data _base; $this->coding= $coding; $this->connect ();//Initialize Connection } /********************************************************************************************* * Database * Basic Method ********************************************************************************************/ Database connection Function Connect () { $link =mysql_connect ($this->host, $this->user_name, $this->user_pwd) or Die ($this->error ()); mysql_select_db ($this->data_base, $link) or die ("Unable to connect to the database". $this->data_base); mysql_query ("Set names ' $this->coding '"); } Error message Function error () { return Mysql_error (); } Mysql_query () method function query ($sql, $type = ') { if (!) ( $query = mysql_query ($sql))) { $this->show (' say: ', $sql); } echo $sql. " <br/> "//Comments after test completion return $query; } SQL statement Display Function Show ($message = ', $sql = ') { if (! $sql) { Echo $message; } Else { echo $message. ' <br> '. $sql; } } Mysql_affected_rows () method function Affected_rows () { return Mysql_affected_rows (); } Mysql_result method function result ($query, $row) { Return mysql_result ($query, $row); } Mysql_num_rows method function Num_rows ($query) { Return @mysql_num_rows ($query); } Mysql_num_fields method function Num_fields ($query) { Return Mysql_num_fields ($query); } Mysql_free_result method function Free_result ($query) { Return Mysql_free_result ($query); } mysql_insert_id method function insert_id () { return mysql_insert_id (); } Mysql_fetch_row method function Fetch_row ($query) { Return mysql_fetch_row ($query); } Mysql_get_server_info method Function version () { return Mysql_get_server_info (); } Mysql_fetch_array () method function Fetch_array ($result) { return mysql_fetch_array ($result); } Mysql_close method function Close () { return Mysql_close (); } /********************************************************************* * Database * Functional Approach *********************************************************************/ /* * Insert Method * $table Table name * $fields Field Name * $value field value */ function Fn_insert ($table, $fields, $values) { return $this->query (insert into $table ($fields) VALUES ($values)); $this->close (); } /* * Select method * $table Table name * $fields Field Name * $condition Query conditions * $order Sorting criteria * $limit out the number of strips */ function Fn_select ($table, $fields, $condition, $order, $limit) { $query = "Select $fields from $table"; if ($condition!= "") { $query. = "where $condition"; } if ($order!= "") { $query. = "ORDER by $order"; } if ($limit!= "") { $query. = "Limit $limit"; } return $this->query ($query); $this->close (); } /* * Delete method * $table Table name * $fields Field Name * $values field value */ function Fn_delete ($table, $condition) { return $this->query ("Delete from $table where $condition"); $this->close (); } /* * Update method * $table Table name * $fields Field Name * $values field value */ function Fn_update ($table, $set, $condition) { $sql = "Update $table set $set"; if ($condition!= "") { $sql. = "where $condition"; } return $this->query ($sql); $this->close (); } /* * destructor, garbage collection */ function __destruct () { echo "Clear"; } }
|