php mysql數(shù)據(jù)庫操作類
更新時(shí)間:2008年06月04日 19:52:49 作者:
mysql數(shù)據(jù)庫 DB類 ,方便學(xué)習(xí)php的朋友,用類方便擴(kuò)展,不建議初級(jí)用戶,可以等熟悉了mysql的數(shù)據(jù)庫操作函數(shù)以后再用,要不以后函數(shù)都記亂了
復(fù)制代碼 代碼如下:
<?php
/*
* mysql數(shù)據(jù)庫 DB類
* @package db
* @author yytcpt(無影)
* @version 2008-03-27
* @copyrigth http://www.d5s.cn/
*/
class db {
var $connection_id = "";
var $pconnect = 0;
var $shutdown_queries = array();
var $queries = array();
var $query_id = "";
var $query_count = 0;
var $record_row = array();
var $failed = 0;
var $halt = "";
var $query_log = array();
function connect($db_config){
if ($this->pconnect){
$this->connection_id = mysql_pconnect($db_config["hostname"], $db_config["username"], $db_config["password"]);
}else{
$this->connection_id = mysql_connect($db_config["hostname"], $db_config["username"], $db_config["password"]);
}
if ( ! $this->connection_id ){
$this->halt("Can not connect MySQL Server");
}
if ( ! @mysql_select_db($db_config["database"], $this->connection_id) ){
$this->halt("Can not connect MySQL Database");
}
if ($db_config["charset"]) {
@mysql_unbuffered_query("SET NAMES '".$db_config["charset"]."'");
}
return true;
}
//發(fā)送SQL 查詢,并返回結(jié)果集
function query($query_id, $query_type='mysql_query'){
$this->query_id = $query_type($query_id, $this->connection_id);
$this->queries[] = $query_id;
if (! $this->query_id ) {
$this->halt("查詢失敗:\n$query_id");
}
$this->query_count++;
$this->query_log[] = $str;
return $this->query_id;
}
//發(fā)送SQL 查詢,并不獲取和緩存結(jié)果的行
function query_unbuffered($sql=""){
return $this->query($sql, 'mysql_unbuffered_query');
}
//從結(jié)果集中取得一行作為關(guān)聯(lián)數(shù)組
function fetch_array($sql = ""){
if ($sql == "") $sql = $this->query_id;
$this->record_row = @mysql_fetch_array($sql, MYSQL_ASSOC);
return $this->record_row;
}
function shutdown_query($query_id = ""){
$this->shutdown_queries[] = $query_id;
}
//取得結(jié)果集中行的數(shù)目,僅對 INSERT,UPDATE 或者 DELETE
function affected_rows() {
return @mysql_affected_rows($this->connection_id);
}
//取得結(jié)果集中行的數(shù)目,僅對 SELECT 語句有效
function num_rows($query_id="") {
if ($query_id == "") $query_id = $this->query_id;
return @mysql_num_rows($query_id);
}
//返回上一個(gè) MySQL 操作中的錯(cuò)誤信息的數(shù)字編碼
function get_errno(){
$this->errno = @mysql_errno($this->connection_id);
return $this->errno;
}
//取得上一步 INSERT 操作產(chǎn)生的 ID
function insert_id(){
return @mysql_insert_id($this->connection_id);
}
//得到查詢次數(shù)
function query_count() {
return $this->query_count;
}
//釋放結(jié)果內(nèi)存
function free_result($query_id=""){
if ($query_id == "") $query_id = $this->query_id;
@mysql_free_result($query_id);
}
//關(guān)閉 MySQL 連接
function close_db(){
if ( $this->connection_id ) return @mysql_close( $this->connection_id );
}
//列出 MySQL 數(shù)據(jù)庫中的表
function get_table_names(){
global $db_config;
$result = mysql_list_tables($db_config["database"]);
$num_tables = @mysql_numrows($result);
for ($i = 0; $i < $num_tables; $i++) {
$tables[] = mysql_tablename($result, $i);
}
mysql_free_result($result);
return $tables;
}
//從結(jié)果集中取得列信息并作為對象返回,取得所有字段
function get_result_fields($query_id=""){
if ($query_id == "") $query_id = $this->query_id;
while ($field = mysql_fetch_field($query_id)) {
$fields[] = $field;
}
return $fields;
}
//錯(cuò)誤提示
function halt($the_error=""){
$message = $the_error."<br/>\r\n";
$message.= $this->get_errno() . "<br/>\r\n";
$sql = "INSERT INTO `db_error`(pagename, errstr, timer) VALUES('".$_SERVER["PHP_SELF"]."', '".addslashes($message)."', ".time().")";
@mysql_unbuffered_query($sql);
if (DEBUG==true){
echo "<html><head><title>MySQL 數(shù)據(jù)庫錯(cuò)誤</title>";
echo "<style type=\"text/css\"><!--.error { font: 11px tahoma, verdana, arial, sans-serif, simsun; }--></style></head>\r\n";
echo "<body>\r\n";
echo "<blockquote>\r\n";
echo "<textarea class=\"error\" rows=\"15\" cols=\"100\" wrap=\"on\" >" . htmlspecialchars($message) . "</textarea>\r\n";
echo "</blockquote>\r\n</body></html>";
exit;
}
}
function __destruct(){
$this->shutdown_queries = array();
$this->close_db();
}
function sql_select($tbname, $where="", $limit=0, $fields="*", $orderby="id", $sort="DESC"){
$sql = "SELECT ".$fields." FROM `".$tbname."` ".($where?" WHERE ".$where:"")." ORDER BY ".$orderby." ".$sort.($limit ? " limit ".$limit:"");
return $sql;
}
function sql_insert($tbname, $row){
foreach ($row as $key=>$value) {
$sqlfield .= $key.",";
$sqlvalue .= "'".$value."',";
}
return "INSERT INTO `".$tbname."`(".substr($sqlfield, 0, -1).") VALUES (".substr($sqlvalue, 0, -1).")";
}
function sql_update($tbname, $row, $where){
foreach ($row as $key=>$value) {
$sqlud .= $key."= '".$value."',";
}
return "UPDATE `".$tbname."` SET ".substr($sqlud, 0, -1)." WHERE ".$where;
}
function sql_delete($tbname, $where){
return "DELETE FROM `".$tbname."` WHERE ".$where;
}
//新增加一條記錄
function row_insert($tbname, $row){
$sql = $this->sql_insert($tbname, $row);
return $this->query_unbuffered($sql);
}
//更新指定記錄
function row_update($tbname, $row, $where){
$sql = $this->sql_update($tbname, $row, $where);
return $this->query_unbuffered($sql);
}
//刪除滿足條件的記錄
function row_delete($tbname, $where){
$sql = $this->sql_delete($tbname, $where);
return $this->query_unbuffered($sql);
}
/* 根據(jù)條件查詢,返回所有記錄
* $tbname 表名, $where 查詢條件, $limit 返回記錄, $fields 返回字段
*/
function row_select($tbname, $where="", $limit=0, $fields="*", $orderby="id", $sort="DESC"){
$sql = $this->sql_select($tbname, $where, $limit, $fields, $orderby, $sort);
return $this->row_query($sql);
}
//詳細(xì)顯示一條記錄
function row_select_one($tbname, $where, $fields="*", $orderby="id"){
$sql = $this->sql_select($tbname, $where, 1, $fields, $orderby);
return $this->row_query_one($sql);
}
function row_query($sql){
$rs = $this->query($sql);
$rs_num = $this->num_rows($rs);
$rows = array();
for($i=0; $i<$rs_num; $i++){
$rows[] = $this->fetch_array($rs);
}
$this->free_result($rs);
return $rows;
}
function row_query_one($sql){
$rs = $this->query($sql);
$row = $this->fetch_array($rs);
$this->free_result($rs);
return $row;
}
//計(jì)數(shù)統(tǒng)計(jì)
function row_count($tbname, $where=""){
$sql = "SELECT count(id) as row_sum FROM `".$tbname."` ".($where?" WHERE ".$where:"");
$row = $this->row_query_one($sql);
return $row["row_sum"];
}
}
?>
很久沒有發(fā)帖了,把我常用的一些php類文件分享出來。
如果您加了新功能,或者是有改進(jìn),請與大家一起分享。
復(fù)制代碼 代碼如下:
<?php
$db_config["hostname"] = "127.0.0.1"; //服務(wù)器地址
$db_config["username"] = "root"; //數(shù)據(jù)庫用戶名
$db_config["password"] = "root"; //數(shù)據(jù)庫密碼
$db_config["database"] = "wap_blueidea_com"; //數(shù)據(jù)庫名稱
$db_config["charset"] = "utf8";
include('db.php');
$db = new db();
$db->connect($db_config);
//例:查詢表 table_name 中 cid=1的所有記錄。
$row = $db->row_select('table_name', 'cid=1');
?>
更詳細(xì)的使用方法,請參考 db類文件中的注釋。
您可能感興趣的文章:
- PHP實(shí)現(xiàn)的sqlite數(shù)據(jù)庫連接類
- php的mssql數(shù)據(jù)庫連接類實(shí)例
- 整合了前面的PHP數(shù)據(jù)庫連接類~~做成一個(gè)分頁類!
- PHP基于MySQLI函數(shù)封裝的數(shù)據(jù)庫連接工具類【定義與用法】
- php入門之連接mysql數(shù)據(jù)庫的一個(gè)類
- php封裝db類連接sqlite3數(shù)據(jù)庫的方法實(shí)例
- 簡單的php數(shù)據(jù)庫操作類代碼(增,刪,改,查)
- PHP數(shù)據(jù)庫操作之基于Mysqli的數(shù)據(jù)庫操作類庫
- PHP實(shí)現(xiàn)PDO的mysql數(shù)據(jù)庫操作類
- php db類庫進(jìn)行數(shù)據(jù)庫操作
- PHP DB 數(shù)據(jù)庫連接類定義與用法示例
相關(guān)文章
基于PHP+mysql實(shí)現(xiàn)新聞發(fā)布系統(tǒng)的開發(fā)
這篇文章主要介紹了基于PHP+mysql實(shí)現(xiàn)新聞發(fā)布系統(tǒng)的開發(fā),文章通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下 面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08基于thinkphp6.0的success、error實(shí)現(xiàn)方法
這篇文章主要介紹了基于thinkphp6.0的success、error實(shí)現(xiàn)方法,本文分步驟給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11如何用phpmyadmin設(shè)置mysql數(shù)據(jù)庫用戶的權(quán)限
發(fā)現(xiàn)有很多用戶對數(shù)據(jù)庫用戶權(quán)限的設(shè)置不太了解,下面為大家詳細(xì)講解一下如何用 phpMyAdmin 來設(shè)置數(shù)據(jù)庫用戶的權(quán)限2012-01-01Thinkphp通過一個(gè)入口文件如何區(qū)分移動(dòng)端和PC端
這篇文章主要介紹了Thinkphp通過一個(gè)入口文件區(qū)分移動(dòng)端和PC端的方法,需要的的朋友參考下吧2017-04-04PHP中file_get_contents函數(shù)抓取https地址出錯(cuò)的解決方法(兩種方法)
本文通過兩種方法解決PHP中file_get_contents函數(shù)抓取https地址出錯(cuò),需要的朋友可以參考下2015-09-09關(guān)于PHP虛擬主機(jī)概念及如何選擇穩(wěn)定的PHP虛擬主機(jī)
PHP是一種HTML內(nèi)嵌式的語言,是一種在端執(zhí)行的嵌入HTML文檔的腳本語言,語言的風(fēng)格有類似于C語言,現(xiàn)在被很多的網(wǎng)站編程人員廣泛的運(yùn)用。這篇文章給大家分享關(guān)于PHP虛擬主機(jī)概念及如何選擇穩(wěn)定的PHP虛擬主機(jī),感興趣的朋友一起看看吧2018-11-11