Home >Backend Development >PHP Tutorial >PHP class to connect to mysql database (interface implementation)

PHP class to connect to mysql database (interface implementation)

WBOY
WBOYOriginal
2016-07-25 08:56:411671browse
This article introduces the class implemented by PHP to connect to the MySQL database. This class first defines an interface and then implements specific methods. Friends in need can refer to it.

Code sharing for php and mysql connection classes, as follows:

<?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. Example of calling the mysql class:
<?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(); 
?>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn