Heim  >  Artikel  >  Backend-Entwicklung  >  php连接mysql数据库的类(接口实现)

php连接mysql数据库的类(接口实现)

WBOY
WBOYOriginal
2016-07-25 08:56:411591Durchsuche
本文介绍下,php实现的连接mysql数据库的类,本类先定义一个接口,然后再实现具体方法。有需要的朋友参考下吧。

php与mysql连接类的代码分享,如下:

<?php 
/** 
 * @连接mysql数据库的类
 * @filename:Class_Con.inc.php
 *  
 * @example $this -> Link ();  
 */ 

//接口定义
interface Connected 
{ 
 
// Buat Bayangan Pertama 
public function __construct(); 
 
// Buat Bayangan Function Kedua  
public function connect (); 
 
// Buat Bayangan Function Ketiga 
public function error_mysql (); 
 
// Buat Bayangan Function Keempat 
public function db_selected (); 
 
// Buat Bayangan Function Kelima  
public function mysql_close (); 
} 

/** 
 *  
 * 使用接品类操作mysql 
 *  
 * @return Function Dari Interface 
 * @var String Variable  
 */ 

class ConfigureMysql implements Connected{ 
 
/** 
 * @var String 
 */ 
var $_link ; 
 
/** 
 * @var String  
 */ 
var $_Link_Cons ;  
 
/** 
 * @var String  
 */ 
var $_Error;  
 
/** 
 * @var String  
 */ 
var $_DB;  
 
 
// Setting Function Dari Interface 
public function __construct() { 
 
$this ->_Link_Cons = $this ->connect(); 
return $this ->_Link_Cons ; 
} 
 
// Setting Function Kedua Dari Interface  
public function connect () { 
 
$this ->_link = @mysql_connect('localhost' , 'Faizal' , 'XXXXXXXXXXX' , '3306') or die($this->error_mysql ()); 
} 
 
// Settiong Function Ketiga Dari Interface  
public function error_mysql () { 
 
$this ->_Error = "<h2> Masalah Pada Koneksi Ke Jalur Mysql </h2>"; 
 
} 
 
// Settiong Function Keempat Dari Interface  
public function db_selected () { 
 
$this ->_DB = mysql_select_db('XXXXXXXX'); 
if ($this ->_DB != TRUE) { 
return $this ->error_mysql(); 
}else { 
return false ; 
} 
} 
 
// Setting Function Kelima Dari Interface  
public function mysql_close () { 
 
return mysql_close($this ->connect()); 
} 
} 

/** 
 * Gunakan Script Classes Untuk Function Parent::  
 *  
 * @example parent::__Construct(); 
 */ 

class LinkCon extends ConfigureMysql  { 
 
/** 
 * @var String 
 */ 
var $_Con ;  
 
/** 
 * @var String 
 */ 
var $_Db ; 
 
/** 
 * @magic Self:: 
 */ 
var  $_Error_Show ; 

/** 
 * @return Mysql_Close  
 */ 
var $_Close ; 
 
 
// Setting Function Dari Class Yang Di Extends 
public function Conf_Show_Mysql () { 
 
$this ->_Con = parent::__construct(); 
} 
 
// Setting Function Dari Class Yang Di Extends  
public function DB_Selected () { 
 
$this ->_Db = $this ->DB_Selected(); 
return $this ->_Db ; 
} 
 
// Setting Function Dari Class Yang Di Extends  
public function _CloseMysql () { 
 
$this ->_Close = $this ->mysql_close(); 
return $this ->_Close ; 
} 
 
// Set Error  
public function Eroor_Show () { 
 
$this ->_Error_Show = $this ->error_mysql();; 
return true ; 
} 
 
// Akhir Classes  
} 
?>
2,mysql类的调用示例:
<?php 
// Included File Classes Connected  
include("Class_Con.inc.php"); 
 
// Set Varibale Untuk Configure Data Classes  
$_Configure = new LinkCon(); 
 
// Set Variable Function  
$_Configure ->Conf_Show_Mysql(); 
 
// Set Variable Function  
$_Configure ->DB_Selected(); 
 
// Set Variable Function  
$_Configure ->_CloseMysql(); 
 
// Set Variable Function  
$_Configure ->Eroor_Show(); 
?>


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn