|
|
|
Code PHP แบบ OOP เพื่อพัฒนาเว็บไซต์ สำหรับคนที่ต้องการศึกษา ลองเอาไปศึกษากันดูครับ |
|
|
|
|
|
|
|
Code (PHP)
<?PHP
class mysql_config{
var $dbconfig= array(
"hostname" => NULL,
"username" => NULL,
"password" => NULL,
"database" => NULL,
"collation_connection" => NULL,
"character_set" => NULL
);
function mysql_config(){
$GLOBALS['global_mysql_querydb_timer'] = 0;
$GLOBALS['global_mysql_querydb_counter'] = 0;
}
function get_configdb(){
return $this->dbconfig;
}
function set_querydb_counter($value){
global $static_querydb_count;
if(isset($static_querydb_count)){
$static_querydb_count= $value;
}else{
$GLOBALS['global_mysql_querydb_counter']= $value;
}
}
function get_querydb_counter(){
global $static_querydb_count;
if(isset($static_querydb_count)){
return $static_querydb_count;
}else{
return $GLOBALS['global_mysql_querydb_counter'];
}
}
function set_querydb_timer($value){
global $static_query_timer;
if(isset($static_query_timer)){
$static_query_timer=$value;
}else{
$GLOBALS['global_mysql_querydb_timer']=$value;
}
}
function get_querydb_timer(){
global $static_querydb_timer;
if(isset($static_querydb_timer)){
return $static_querydb_timer;
}else{
return $GLOBALS['global_mysql_querydb_timer'];
}
}
function set_character_set($value){
$this->dbconfig['character_set']= $value;
}
function set_collation_connection($value){
$this->dbconfig['collaction_connection']= $value;
}
function set_database($value){
$this->dbconfig['database']= $value;
}
function set_hostname(){
$this->dbconfig['hostname']= $value;
}
function set_username($value){
$this->dbconfig['username']= $value;
}
function set_password($value){
$this->dbconfig['password']= $value;
}
}
class mysql_operator extends mysql_config{
var $link = NULL;
var $result= NULL;
var $mysql_version = NULL;
var $string_error = array();
function set_configdb(&$array_dbconfig){
$this->set_hostname($array_dbconfig['hostname']);
$this->set_username($array_dbconfig['username']);
$this->set_password($array_dbconfig['password']);
$this->set_database($array_dbconfig['database']);
$this->set_collation_connection(
$array_dbconfig['collation_connection']
);
$this->set_character_set($array_dbconfig['character_set']);
}
function set_error_msg($string){
array_push($this->string_error, $string);
}
function get_error_msg(){
while(count($this->string_error)-1)
$string_output='<b>MySql Error:</b>'.
array_pop($this->string_error).'<br/>';
return $string_output;
}
function mysql_operator(&$dbconfig){
if(is_array($dbconfig))$this->set_configdb($dbconfig);
else $this->set_configdb($dbconfig->get_configdb());
}
function set_mysql_version($value){
$this->mysql_version=$value;
}
function get_mysql_version(){
return $this->mysql_version;
}
function opendb($newlink=FALSE){
$this->link=mysql_connect(
$this->dbconfig['hostname'],
$this->dbconfig['username'],
$this->dbconfig['password'],
$newlink);
if(!$this->link)
$this->set_error_msg(
mysql_errno($this->link).':'.
mysql_error($this->link)
);
$this->set_mysql_version(mysql_get_server_info());
if(!mysql_select_db($this->dbconfig['database']))
$this->set_error_msg(
mysql_errno($this->link).':'.
mysql_error($this->link)
);
if(!empty($this->dbconfig['character_set'])){
$this->querydb("SET CHARACTER SET".
$this->dbconfig['character_set'].";"
);
$this->querydb_counter_decrement();
}
if(!empty($this->dbconfig['collation_connection'])){
$this->querydb("SET collation_connection=".
$this->dbconfig['collation_connecttion'].";");
$this->querydb_counter_decrement();
}
}
function closedb(){
if(!mysql_close($this->link))
$this->set_error_msg(
mysql_errno($this->link).':'.
mysql_error($this->link)
);
}
function insert(){
$this->sql = "insert into $this->table ($this->fiel) values ($this->value)";
return $this->sql;
}
function update(){
$this->sql = "update $this->table set $this->fiel where $this->condition";
return $this->sql;
}
function delete(){
$this->sql = "delete from $this->table where $this->condition";
return $this->sql;
}
function querydb($string_query){
$start_time=array_sum(explode(chr(32),microtime()));
$this->result=mysql_query($string_query);
mysql_query("SET NAMES UTF8");
$end_time= array_sum(explode(chr(32),microtime()));
if(!$this->result)
$this->set_error_msg(
mysql_errno($this->link).':'.
mysql_error($this->link)
);
$this->set_querydb_timer(
$this->get_querydb_timer()+($end_time-$start_time)
);
$this->querydb_counter_increment();
}
function querydb_counter_increment(){
$this->set_querydb_counter($this->get_querydb_counter()+1);
}
function querydb_counter_decrement(){
$this->set_querydb_counter($this->get_querydb_counter()-1);
}
function get_resultdb(){
return $this->result;
}
function fetch_data(){
return mysql_fetch_array($this->get_resultdb());
}
function num_rows(){
return mysql_num_rows($this->get_resultdb());
}
}
//connection
$obj_dbconfig = new mysql_config();
$obj_dbconfig->set_hostname("server");
$obj_dbconfig->set_username("username");
$obj_dbconfig->set_password("password");
$obj_dbconfig->set_database("database");
$obj_dbconfig->set_character_set("utf8");
$obj_dbconfig->set_collation_connection("utf8_general_ci");
$obj_db = new mysql_operator($obj_dbconfig->get_configdb());
$obj_db->opendb();
?>
Tag : PHP
|
|
|
|
|
|
Date :
2013-02-20 14:57:37 |
By :
hassatorn |
View :
1817 |
Reply :
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function เยอะจัง เห็นแล้วมันปวดหัวดี
แต่ก็ขอบใจจ๋า
|
|
|
|
|
Date :
2013-02-20 15:23:09 |
By :
KenJeRoKung |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
น่าจะมีตัวอย่างการนำไปใช้มั้งนะครับ
|
|
|
|
|
Date :
2013-02-20 15:47:07 |
By :
MrAeker |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Date :
2013-02-21 09:19:35 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ตัวอย่างว่างๆ เดี๋ยวเอามาให้ครับ แต่ตอนนี้ขอตัวทำงานก่อนเด้อ อิอิ
|
|
|
|
|
Date :
2013-02-21 09:43:00 |
By :
hassatorn |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
วิธีใช้โหลด Ebook จากเว็บเลยครับ
http://www.zone-it.com/b22/185921
|
ประวัติการแก้ไข 2013-02-24 11:01:59
|
|
|
|
Date :
2013-02-24 11:01:33 |
By :
hassatorn |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|