本文章提供了數(shù)據(jù)庫(kù)連接實(shí)例,主要是講php mysql數(shù)據(jù)相關(guān)操作,有需要的朋友可以使用看看,實(shí)例代碼如下:
- <?php
- class mysql {
- private $db_host; //主機(jī)地址
- private $db_user; //用戶名
- private $db_pass; //連接密碼
- private $db_name; //名稱(chēng)
- private $db_charset; //編碼
- private $conn;
- public $debug=false;//調(diào)試開(kāi)關(guān),默認(rèn)關(guān)閉
- private $query_id; //用于判斷sql語(yǔ)句是否執(zhí)行成功
- private $result; //結(jié)果集
- private $num_rows; //結(jié)果集中行的數(shù)目,僅對(duì)select有效
- private $insert_id; //上一步 insert 操作產(chǎn)生的 id
- // 構(gòu)造/析構(gòu)函數(shù)
- 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);
- }
- // 連接/選擇數(shù)據(jù)庫(kù)
- 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('數(shù)據(jù)庫(kù)-連接失敗:用戶名或密碼錯(cuò)誤!');
- }
- if (!@mysql_select_db($this->db_name,$this->conn)) {
- $this->show_error("數(shù)據(jù)庫(kù)-選擇失敗:數(shù)據(jù)庫(kù) $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語(yǔ)句 <b>"$sql"</b> 執(zhí)行時(shí)遇到錯(cuò)誤");
- return $this->query_id;
- }
- // 顯示詳細(xì)錯(cuò)誤信息
- public function show_error ($msg) {
- if($this->debug){
- $errinfo = mysql_error();
- echo "錯(cuò)誤:$msg <br/> 返回:$errinfo<p>";
- }else{
- echo '<p>出現(xiàn)錯(cuò)誤!<p>';
- }
- }
- // 獲得query執(zhí)行成功與否的信息
- 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();
- }
- // 顯示共有多少?gòu)埍?nbsp;
- public function show_tables () {
- $this->query("show tables");
- if ($this->query_id) {
- echo "數(shù)據(jù)庫(kù) $this->db_name 共有 ".$this->num_rows($this->query_id)." 張表<br/>";
- $i = 1;
- while ($row = $this->fetch_array($this->query_id)){
- echo "$i -- $row[0]<br/>";
- $i ++;
- }
- }
- }
- // 顯示共有多少個(gè)數(shù)據(jù)庫(kù)
- public function show_dbs(){
- $this->query("show databases");
- if ($this->query_id) {
- echo "共有數(shù)據(jù)庫(kù) ".$this->num_rows($this->query_id)." 個(gè)<br/>";
- $i = 1;
- while ($this->row = $this->fetch_array($this->query_id)){
- echo "$i -- ".$this->row[database]."<br />";
- $i ++;
- }
- }
- }
- // 刪除數(shù)據(jù)庫(kù):返回刪除結(jié)果
- public function drop_db ($db_name='') {
- if ($db_name == '') {
- $db_name = $this->db_name;//默認(rèn)刪除當(dāng)前數(shù)據(jù)庫(kù)
- $this->query("drop database $db_name");
- }else {
- $this->query("drop database $db_name");
- }
- if ($this->query_id) {
- return "數(shù)據(jù)庫(kù) $db_name 刪除成功";
- }else {
- $this->show_error("數(shù)據(jù)庫(kù) $db_name 刪除失敗");
- }
- }
- // 刪除數(shù)據(jù)表:返回刪除結(jié)果
- public function drop_table ($table_name) {
- $this->query("drop table $table_name");
- if ($this->query_id) {
- return "數(shù)據(jù)表 $table_name 刪除成功";
- }else {
- $this->show_error("數(shù)據(jù)表 $table_name 刪除失敗");
- }
- }
- // 創(chuàng)建數(shù)據(jù)庫(kù)
- public function create_db ($db_name) {
- $this->query("create database $db_name");
- if($this->query_id){
- return "數(shù)據(jù)庫(kù) $db_name 創(chuàng)建成功";
- }else {
- $this->show_error("數(shù)據(jù)庫(kù) $db_name 創(chuàng)建失敗");
- } //開(kāi)源代碼Vevb.com
- }
- // 獲取數(shù)據(jù)庫(kù)版本
- public function get_info(){
- echo mysql_get_server_info();
- }
- // 釋放內(nèi)存
- public function free_result () {
- if ( @mysql_free_result($this->query_id) )
- unset ($this->result);
- $this->query_id = 0;
- }
- } // end class
- ?>
下面提供一款自動(dòng)選擇數(shù)據(jù)庫(kù)遠(yuǎn)程或本地連接代碼:
- <?php
- // 包含mysql操作類(lèi)
- include_once 'mysql.class.php';
- // 本地mysql數(shù)據(jù)
- $mysql_local_data = array('db_host'=>'localhost',
- 'db_user'=>'root',
- 'db_pass'=>'root',
- 'db_name'=>'test');
- // 遠(yuǎn)程mysql數(shù)據(jù)
- $mysql_remote_data = array('db_host'=>'61.183.41.178',
- 'db_user'=>'xxx',
- 'db_pass'=>'xxx',
- 'db_name'=>'xxx');
- // 公用數(shù)據(jù)
- $tb_prefix = 'php95_';
- $db_charset = 'utf-8';
- //本地連接成功則實(shí)例化本地mysql類(lèi),否則連接遠(yuǎn)程數(shù)據(jù)庫(kù)并實(shí)例化mysql類(lèi)
- 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(); //測(cè)試:顯示當(dāng)前數(shù)據(jù)庫(kù)下的所有表名
- ?>
假設(shè)我們要在test.php文件中操作虛擬主機(jī)的數(shù)據(jù)庫(kù),則首先要在本地調(diào)試,那么必然要連接本地、遠(yuǎn)程兩個(gè)不同的數(shù)據(jù)庫(kù),問(wèn)題:怎么讓test.php自動(dòng)識(shí)別當(dāng)下該連接本地還是遠(yuǎn)程數(shù)據(jù)庫(kù)呢?
新聞熱點(diǎn)
疑難解答