一款php資料庫連接類_PHP教程

來源:互聯網
上載者:User
本文章提供了二款資料庫教程串連執行個體,主要是講php教程 mysql教程資料相關操作,有需要的朋友可以使用看看。

class mysql {
private $db_host; //主機地址
private $db_user; //使用者名稱
private $db_pass; //串連密碼
private $db_name; //名稱
private $db_charset; //編碼
private $conn;
public $debug=false;//調試開關,預設關閉
private $query_id; //用於判斷sql語句是否執行成功
private $result; //結果集
private $num_rows; //結果集中行的數目,僅對select有效
private $insert_id; //上一步 insert 操作產生的 id
// 構造/解構函式
function __construct ($db_host,$db_user,$db_pass,$db_name,$db_charset,$conn) {
$this->db_host = $db_host ;
$this->db_user = $db_user ;
$this->db_pass = $db_pass ;
$this->db_name = $db_name ;
$this->db_charset = $db_charset ;
$this->conn = $conn ;
$this->connect();
}
function __destruct () {
@mysql_close($this->conn);
}
// 串連/選擇資料庫
public function connect () {
if ($this->conn == 'pconn') {
@$this->conn = mysql_pconnect($this->db_host,$this->db_user,$this->db_pass);
} else {
@$this->conn = mysql_connect($this->db_host,$this->db_user,$this->db_pass);
}
if (!$this->conn) {
$this->show_error('資料庫-串連失敗:使用者名稱或密碼錯誤!');
}
if (!@mysql_select_db($this->db_name,$this->conn)) {
$this->show_error("資料庫-選擇失敗:資料庫 $this->db_name 不可用");
}
mysql_query("set names $this->db_charset");
return $this->conn;
}
// query方法
public function query ($sql) {
if ($this->query_id) $this->free_result();
$this->query_id = @mysql_query($sql,$this->conn);
if (!$this->query_id) $this->show_error("sql語句 "$sql" 執行時遇到錯誤");
return $this->query_id;
}
// 顯示詳細錯誤資訊
public function show_error ($msg) {
if($this->debug){
$errinfo = mysql_error();
echo "錯誤:$msg
返回:$errinfo

";
}else{
echo '

出現錯誤!

';
}
}
// 獲得query執行成功與否的資訊
public function get_query_info($info){
if ($this->query_id) {
echo $info;
}
}
// 查詢所有
public function findall ($table_name) {
$this->query("select * from $table_name");
}
// mysql_fetch_array
public function fetch_array () {
if ($this->query_id) {
$this->result = mysql_fetch_array($this->query_id);
return $this->result;
}
}
// ......
public function fetch_assoc () {
if ($this->query_id) {
$this->result = mysql_fetch_assoc($this->query_id);
return $this->result;
}
}
public function fetch_row () {
if ($this->query_id) {
$this->result = mysql_fetch_row($this->query_id);
return $this->result;
}
}
public function fetch_object () {
if ($this->query_id) {
$this->result = mysql_fetch_object($this->query_id);
return $this->result;
}
}
// 擷取 num_rows
public function num_rows () {
if ($this->query_id) {
$this->num_rows = mysql_num_rows($this->query_id);
return $this->num_rows;
}
}
// 擷取 insert_id
public function insert_id () {
return $this->insert_id = mysql_insert_id();
}
// 顯示共有多少張表
public function show_tables () {
$this->query("show tables");
if ($this->query_id) {
echo "資料庫 $this->db_name 共有 ".$this->num_rows($this->query_id)." 張表
";
$i = 1;
while ($row = $this->fetch_array($this->query_id)){
echo "$i -- $row[0]
";
$i ++;
}
}
}
// 顯示共有多少個資料庫
public function show_dbs(){
$this->query("show databases");
if ($this->query_id) {
echo "共有資料庫 ".$this->num_rows($this->query_id)." 個
";
$i = 1;
while ($this->row = $this->fetch_array($this->query_id)){
echo "$i -- ".$this->row[database]."
";
$i ++;
}
}
}
// 刪除資料庫:返回刪除結果
public function drop_db ($db_name='') {
if ($db_name == '') {
$db_name = $this->db_name;//預設刪除當前資料庫
$this->query("drop database $db_name");
}else {
$this->query("drop database $db_name");
}
if ($this->query_id) {
return "資料庫 $db_name 刪除成功";
}else {
$this->show_error("資料庫 $db_name 刪除失敗");
}
}
// 刪除資料表:返回刪除結果
public function drop_table ($table_name) {
$this->query("drop table $table_name");
if ($this->query_id) {
return "資料表 $table_name 刪除成功";
}else {
$this->show_error("資料表 $table_name 刪除失敗");
}
}
// 建立資料庫
public function create_db ($db_name) {
$this->query("create database $db_name");
if($this->query_id){
return "資料庫 $db_name 建立成功";
}else {
$this->show_error("資料庫 $db_name 建立失敗");
}
}
// 擷取資料庫版本
public function get_info(){
echo mysql_get_server_info();
}
// 釋放記憶體
public function free_result () {
if ( @mysql_free_result($this->query_id) )
unset ($this->result);
$this->query_id = 0;
}
} // end class
?>

下面提供一款自動選擇資料庫遠程或本地串連代碼

// 包含mysql操作類
include_once 'mysql.class.php';
// 本地mysql資料
$mysql_local_data = array('db_host'=>'localhost',
'db_user'=>'root',
'db_pass'=>'root',
'db_name'=>'test');
// 遠程mysql資料
$mysql_remote_data = array('db_host'=>'61.183.41.178',
'db_user'=>'xxx',
'db_pass'=>'xxx',
'db_name'=>'xxx');
// 公用資料
$tb_prefix = 'php95_';
$db_charset = 'utf-8';
//本地串連成功則執行個體化本地mysql類,否則串連遠端資料庫並執行個體化mysql類
if (@mysql_connect($mysql_local_data[db_host], $mysql_local_data[db_user], $mysql_local_data[db_pass]))
$db = new mysql($db_host, $mysql_local_data[db_user], $mysql_local_data[db_pass], $mysql_local_data[db_name], $db_charset, $conn);
else
$db = new mysql($mysql_remote_data[db_host], $mysql_remote_data[db_user], $mysql_remote_data[db_pass], $mysql_remote_data[db_name], $db_charset, $conn);
$db->show_tables(); //測試:顯示當前資料庫下的所有表名
?>

假設我們要在test.php檔案中操作虛擬機器主機的資料庫,則首先要在本地調試,那麼必然要串連本地、遠程兩個不同的資料庫,問題:怎麼讓test.php自動識別當下該串連本地還是遠端資料庫呢?

http://www.bkjia.com/PHPjc/630771.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/630771.htmlTechArticle本文章提供了二款資料庫教程串連執行個體,主要是講php教程 mysql教程資料相關操作,有需要的朋友可以使用看看。 ?php class mysql { private $db_h...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.