這是一款PHP與mysql數據庫連接文件代碼,如果你正在找這樣功能的代碼,可以進來看看,非常完整文件,實例代碼如下:
- class mysql {
- private $db_host; //主機地址
- private $db_user; //用戶名
- private $db_pass; //連接密碼
- private $db_name; //名稱
- private $db_charset; //編碼
- private $conn;
- 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('數據庫-連接失?。河脩裘蛎艽a錯誤!');
- }
- 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語句 <b>"$sql"</b> 執行時遇到錯誤");
- return $this->query_id;
- }
- // 查詢所有
- 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)." 張表<br/>";
- $i = 1;
- while ($row = $this->fetch_array($this->query_id)){
- echo "$i -- $row[0]<br/>";
- $i ++;
- }
- }
- }
- // 顯示共有多少個數據庫
- public function show_dbs(){
- $this->query("show databases");
- if ($this->query_id) {
- echo "共有數據庫 ".$this->num_rows($this->query_id)." 個<br/>";
- $i = 1;
- while ($this->row = $this->fetch_array($this->query_id)){
- echo "$i -- ".$this->row[database]."<br />";
- $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 show_error ($msg) {
- $errinfo = mysql_error();
- echo "錯誤:$msg <br/> 返回:$errinfo<p>";
- }//開源代碼Vevb.com
- // 釋放內存
- public function free_result () {
- if ( @mysql_free_result($this->query_id) )
- unset ($this->result);
- $this->query_id = 0;
- }
- } // end class
新聞熱點
疑難解答