php的Mysql資料庫連線類
<?php
/** * 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('錯誤提示:連結資料庫失敗!'); } } if(
[email protected]_select_db($this->db_database, $this->conn)){ //開啟資料庫失敗 if($this->show_error){ $this->show_error('錯誤提示:開啟資料庫失敗!'); } } if([email protected]_query("set names $this->coding")){ //設定編碼失敗 if($this->show_error){ $this->show_error('錯誤提示:設定編碼失敗!'); } } } /** * 可執行查詢新增修改刪除等任何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++; } } //以陣列形式返回主機中所有資料庫名 public function databases() { $rsPtr = @mysql_list_dbs($this->conn); $i = 0; $cnt = mysql_num_rows($rsPtr); while ($i < $cnt) { $rs[] = mysql_db_name($rsPtr, $i); $i++; } return $rs; } /** * 查詢資料庫下所有表名 * * @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); } //獲取關聯陣列,使用$row['欄位名'] public function fetch_assoc() { return mysql_fetch_assoc($this->result); } //獲取數字索引陣列,使用$row[0],$row[1],$row[2] public function fetch_row() { return mysql_fetch_row($this->result); } //獲取物件陣列,使用$row->content public function fetch_Object() { return mysql_fetch_object($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"); } //簡化查詢select public function select($table, $columnName = "*", $condition = '', $debug = '') { $condition = $condition ? ' Where ' . $condition : NULL; if ($debug) { echo "SELECT $columnName FROM $table $condition"; } else { return $this->query("SELECT $columnName FROM $table $condition"); } } /** * 簡化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>"; echo "</fieldset>"; } }
相關推薦
人家的Python資料庫連線類和sql語句拼接方法
原文出處: sql拼接方法 # encoding=utf-8 from django.http import HttpResponse from anyjson import serialize from django.http import HttpRespon
自己寫的Python資料庫連線類和sql語句拼接方法
這個工具類十分簡單和簡潔。 sql拼接方法 # encoding=utf-8 from django.http import HttpResponse from anyjson import serialize from django.http import HttpRespo
php的Mysql資料庫連線類
<?php/** * mysql資料庫連線類 */ class Mysql{ private $db_host; //資料庫主機 private $db_user; //資料庫使用者名稱 private $db_pwd; //資料庫密碼 private $
.net用工廠模式開發多資料庫連線類
using System;using System.Collections.Generic;using System.Text;using System.Data;using System.Data.OracleClient;using System.Configuration;//工廠模式連線資料庫中的OR
JFinal配置資料庫連線池外掛和表類對映
配置資料庫連線池外掛,此處以Druid為例,還需要配置資料庫訪問外掛,即ActiveRecord外掛,用於建立資料庫中Table和Java Bean的mapping對映: public void configPlugin(Plugins me) { // 配置 druid
python連線各種資料庫的類方法
連線postgresql資料庫 import psycopg2 # 用來操作資料庫的類 class GPCommand(object): # 類的初始化 def __init__(self): self.hostname = '10.1.2.xx'
JDBC連線資料庫工具類以及測試
1. 資料庫連線工具程式碼 package com.zzm.db; import java.sql.*; /** * Created by ming on 2017/6/13. */ public class DBUtil { //載入驅動 private f
ThreadLocal解決事務執行緒安全問題(c3p0資料庫連線池工具類)
ThreadLocal底層是Map集合,它的key是當前執行緒,value由自己設定,可以繫結Connection或其他物件等,保證本次同一執行緒使用同一Connection。 ThreadLocal類提供幾個方法: get/set/remove 以下是ThreadLocal搭配c3p
簡單實現jdbc連線資料庫工具類
第一步:匯入連線mysql資料庫所需要的jar包 第二步:實現一個簡單的jdbc連線資料庫工具類 package jdbc; import java.sql.Connection; import java.sql.DriverManager; import ja
事務與資料庫連線池DBCP和C3P0與工具類DBUtils
文章目錄 事務 使用命令列方式演示事務。 使用程式碼方式演示事務 事務的特性 事務的安全隱患 讀未提交 演示 讀已提交演示 可序列化
PHP環境配置XAMPP,資料庫連線公共類
(1)XAMPP下載地址: https://www.apachefriends.org/download.html (2)安裝各個模組(xdebug) php配置php.ini檔案 apache配置apache.conf (3)資料庫類 <?php &n
Stoker的Java學習之封裝資料庫連線工具類與commons-dbutils
Java學習之封裝資料庫連線工具類與commons-dbutils 一.封裝資料庫連結工具類 public class JDBCUitl { // 宣告連結 private static Connection connection; // 註冊驅動 st
java基礎類庫學習 java.sql(7)使用資料庫連線池來管理資料庫連線物件
前言: 在實際開發中,如果我們不斷地建立資料庫連線物件,一個數據庫連線物件均對應一個物理資料庫連線,每次操作都開啟一個物理連線,使用完後就立即關閉連線,頻繁的開啟關閉連線會造成系統性能下降 因此實際開發中不推薦頻繁的建立資料庫連線物件,頻繁的開啟物理資料庫連線,頻繁的關閉
一個關於php使用pdo方式進行資料庫連線和處理的類
話不多說,先貼程式碼 <?php /** @DB Operates For PDO @author:MeeeeN @date:2015-10-22 22:40:32 **/ //定義資料庫資訊 header("Content-type:text/
資料庫連線及操作類
using System.Configuration;//需要在“引用”中也新增 using System.Data.SqlClient;//SQL資料 using System.Data; using System; using System.Windows.Forms; namesp
玩轉JDBC打造資料庫操作萬能工具類JDBCUtil,加入了高效的資料庫連線池,利用了引數繫結有效防止SQL注入
SELECT * FROM emp_test 成功查詢到了14行資料 第1行:{DEPT_TEST_ID=10, EMP_ID=1001, SALARY=10000, HIRE_DATE=2010-01-12, BONUS=2000, MANAGER=1005, JOB=Manager, NAME=張無忌}
jdbc連線mysql連線資料庫工具類
以往寫程式碼,凡是牽扯到資料庫連線的,尤其是專案中的DAO層,每寫一個方法都要重複步驟:載入資料庫驅動,建立連線……使得程式碼十分的繁瑣,又很浪費時間,於是今天想了想,寫了一個連線資料庫的工具類,以便以後使用,經過測試,功能可以實現,其中的各個方法均可執行 程式碼如下 imp
java連線MySQL資料庫DB類底層框架程式碼實現
前言:用java連線MySQL資料庫程式碼都是十分固定的,所以直接將一些固定的程式碼寫在一個DBmanager類中可以省去許多重複的工作,連線不同MySQL資料庫只需改變user和password即可。為了方便大家,我已經將程式碼總結出來。 程式碼塊 i
關於idea SpringBoot專案中出現與資料庫連線載入不到驅動類異常
解決問題的關鍵看pom.xml是否配置,還有就是版本號要寫上,有些Mysql預設版本不被支援,我的Mysql就出現這個情況,配上version版本就好了看了半天部落格,不知道哪錯了,軟體解除安裝了又裝還是不行,結果一個Bug出現在沒配置版本號上,以後引以為戒!<depe
030 DBUtils工具類與DataSource資料庫連線池
1. DButils工具類的介紹個三個核心類 1. DButils工具類的介紹個三個核心類 a: 概述 DBUtils是java程式設計中的資料庫操作實用工具,小巧簡單實用。 DBUtils封裝了對JDBC的操作,簡化了JDBC操作,可以少寫