這是一款比較全的mysql操作類,昨天寫了一個簡單的連接mysql數據庫代碼,相對于這個來說,那個是最簡單的了,這個是一款包括數據查詢,更新,刪除,等操作,實例代碼如下:
- class mysql{
- private $db_host; //數據庫主機
- private $db_user; //數據庫用戶名
- private $db_pwd; //數據庫密碼
- private $db_database; //數據庫名
- private $conn; //數據庫連接標識;
- private $sql; //sql執行的語句
- private $result; //query的資源標識符
- private $coding; //數據庫編碼,gbk,utf8,gb2312
- private $show_error = true; //本地調試使用,打印錯誤
- /**
- * 構造函數
- *
- * @access public
- * @parameter string $db_host 數據庫主機
- * @parameter string $db_user 數據庫用戶名
- * @parameter string $db_pwd 數據庫密碼
- * @parameter string $db_database 數據庫名
- * @parameter string $coding 編碼
- * @return void
- */
- public function __construct($db_host, $db_user, $db_pwd, $db_database, $coding){
- $this->db_host = $db_host;
- $this->db_user = $db_user;
- $this->db_pwd = $db_pwd;
- $this->db_database = $db_database;
- $this->coding = $coding;
- $this->connect();
- }
- /**
- * 鏈接數據庫
- *
- * @access private
- * @return void
- */
- private function connect(){
- $this->conn = @mysql_connect($this->db_host,$this->db_user,$this->db_pwd);
- if(!$this->conn){
- //show_error開啟時,打印錯誤
- if($this->show_error){
- $this->show_error('錯誤提示:鏈接數據庫失?。?#39;);
- }
- }
- if(!@mysql_select_db($this->db_database, $this->conn)){
- //打開數據庫失敗
- if($this->show_error){
- $this->show_error('錯誤提示:打開數據庫失?。?#39;);
- }
- }
- if(!@mysql_query("set names $this->coding")){
- //設置編碼失敗
- if($this->show_error){
- $this->show_error('錯誤提示:設置編碼失?。?#39;);
- }
- }
- }
- /**
- * 可執行查詢添加修改刪除等任何sql語句
- *
- * @access public
- * @parameter string $sql sql語句
- * @return resource 資源標識符
- */
- public function query($sql){
- $this->sql = $sql;
- $result = mysql_query($this->sql, $this->conn);
- if(!$result){
- //query執行失敗,打印錯誤
- $this->show_error("錯誤的sql語句:", $this->sql);
- }else{
- //返回資源標識符
- return $this->result = $result;
- }
- }
- /**
- * 查詢mysql服務器中所有的數據庫
- *
- * @access public
- * @return void
- */
- public function show_databases(){
- $this->query("show databases");
- //打印數據庫的總數
- echo "現有數據庫:" . mysql_num_rows($this->result);
- echo "<br />";
- $i = 1;
- //循環輸出每個數據庫的名稱
- while($row=mysql_fetch_array($this->result)){
- echo "$i $row[database]" . "<br />";
- $i++;
- }
- }
- /**
- * 查詢數據庫下所有表名
- *
- * @access public
- * @return void
- */
- public function show_tables(){
- $this->query("show tables");
- //打印表的總數
- echo "數據庫{$this->db_database}共有" . mysql_num_rows($this->result) . "張表:";
- echo "<br />";
- //構造數組下標,循環出數據庫所有表名
- $column_name = "tables_in_" . $this->db_database;
- $i = 1;
- //循環輸出每個表的名稱
- while($row=mysql_fetch_array($this->result)){
- echo "$i $row[$column_name]" . "<br />";
- $i++;
- }
- }
- /**
- * 取得記錄集,獲取數組-索引和關聯
- *
- * @access public
- * @return void
- */
- public function fetch_array(){
- return mysql_fetch_array($this->result);
- }
- /**
- * 簡化select查詢語句
- *
- * @access public
- * @parameter string $table 表名
- * @parameter string $field 字段名
- * @return resource
- */
- public function findall($table, $field = '*') {
- return $this->query("select $field from $table");
- }
- /**
- * 簡化delete查詢語句
- *
- * @access public
- * @parameter string $table 表名
- * @parameter string $condition 查詢的條件
- * @return resource
- */
- public function delete($table, $condition) {
- return $this->query("delete from $table where $condition");
- }
- /**
- * 簡化insert插入語句
- *
- * @access public
- * @parameter string $table 表名
- * @parameter string $field 字段名
- * @parameter string $value 插入值
- * @return resource
- */
- public function insert($table, $field, $value) {
- return $this->query("insert into $table ($field) values ('$value')");
- }
- /**
- * 簡化update插入語句
- *
- * @access public
- * @parameter string $table 表名
- * @parameter string $update_content 更新的內容
- * @parameter string $condition 條件
- * @return resource
- */
- public function update($table, $update_content, $condition) {
- return $this->query("update $table set $update_content where $condition");
- }
- /**
- * 取得上一步 insert 操作產生的 id
- *
- * @access public
- * @return integer
- */
- public function insert_id() {
- return mysql_insert_id();
- }
- /**
- * 計算結果集條數
- *
- * @access public
- * @return integer
- */
- public function num_rows() {
- return mysql_num_rows($this->result);
- }
- /**
- * 查詢字段數量和字段信息
- *
- * @access public
- * @parameter string $table 表名
- * @return void
- */
- public function num_fields($table) {
- $this->query("select * from $table");
- echo "<br />";
- //打印字段數
- echo "字段數:" . $total = mysql_num_fields($this->result);
- echo "<pre>";
- //mysql_fetch_field() 函數從結果集中取得列信息并作為對象返回。
- for ($i = 0; $i < $total; $i++) {
- print_r(mysql_fetch_field($this->result, $i));
- }
- echo "</pre>";
- echo "<br />";
- }
- /**
- * 輸出sql語句錯誤信息
- *
- * @access public
- * @parameter string $message 提示信息
- * @return void
- */
- public function show_error($message='',$sql=''){
- echo "<fieldset>";
- echo "<legend>錯誤信息提示:</legend><br />";
- echo "<div style='font-size:14px; clear:both; font-family:verdana, arial, helvetica, sans-serif;'>";
- //打印錯誤原因
- echo "錯誤原因:" . mysql_error() . "<br /><br />";
- //打印錯誤信息
- //mysql_error() 函數返回上一個 mysql 操作產生的文本錯誤信息。
- echo "<div style='height:20px; background:#ff0000; border:1px #ff0000 solid'>";
- echo "<font color='white'>" . $message . "</font>";
- echo "</div>";
- //打印錯誤sql語句
- echo "<font color='red'><pre>" . $sql . "</pre></font>";
- echo "</div>";//開源代碼Vevb.com
- echo "</fieldset>";
- }
- }
- //使用方法
- $mysql = new mysql($dbhost, $dbuser, $dbpwd, $dbname, $coding);
新聞熱點
疑難解答