php mysql資料庫連接類_PHP教程

來源:互聯網
上載者:User
提供一款簡單實現的串連類是利用php 建構函式自動建立串連與刪除操作,有需要的朋友可以參考。
代碼如下 複製代碼

class mysql {
private $db_host; //資料庫主機
private $db_user; //資料庫使用者名稱
private $db_pwd; //資料庫使用者名稱密碼
private $db_database; //資料庫名
private $conn; //資料庫連接標識;
private $result; //執行query命令的結果資源標識
private $sql; //sql執行語句
private $row; //返回的條目數
private $coding; //資料庫編碼,GBK,UTF8,gb2312
private $bulletin = true; //是否開啟錯誤記錄
private $show_error = true; //測試階段,顯示所有錯誤,具有安全隱患,預設關閉
private $is_error = false; //發現錯誤是否立即終止,預設true,建議不啟用,因為當有問題時使用者什麼也看不到是很苦惱的

/*建構函式*/
public function __construct($db_host, $db_user, $db_pwd, $db_database, $conn, $coding) {
$this->db_host = $db_host;
$this->db_user = $db_user;
$this->db_pwd = $db_pwd;
$this->db_database = $db_database;
$this->conn = $conn;
$this->coding = $coding;
$this->connect();
}

/*資料庫連接*/
public function connect() {
if ($this->conn == "pconn") {
//永久連結
$this->conn = mysql_pconnect($this->db_host, $this->db_user, $this->db_pwd);
} else {
//即使連結
$this->conn = mysql_connect($this->db_host, $this->db_user, $this->db_pwd);
}

if (!mysql_select_db($this->db_database, $this->conn)) {
if ($this->show_error) {
$this->show_error("資料庫不可用:", $this->db_database);
}
}
mysql_query("SET NAMES $this->coding");
}

/*資料庫執行語句,可執行查詢添加修改刪除等任何sql語句*/
public function query($sql) {
if ($sql == "") {
$this->show_error("SQL語句錯誤:", "SQL查詢語句為空白");
}
$this->sql = $sql;

$result = mysql_query($this->sql, $this->conn);

if (!$result) {
//調試中使用,sql語句出錯時會自動列印出來
if ($this->show_error) {
$this->show_error("錯誤SQL語句:", $this->sql);
}
} else {
$this->result = $result;
}
return $this->result;
}

/*建立添加新的資料庫*/
public function create_database($database_name) {
$database = $database_name;
$sqlDatabase = 'create database ' . $database;
$this->query($sqlDatabase);
}

http://www.bkjia.com/PHPjc/631326.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/631326.htmlTechArticle提供一款簡單實現的串連類是利用php 建構函式自動建立串連與刪除操作,有需要的朋友可以參考。 代碼如下 複製代碼 class mysql { private $d...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.