代碼拷貝框
<?PHP
/*
* MySQL數據庫 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;
}
//發送SQL 查詢,並返回結果集
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;
}
//發送SQL 查詢,並不獲取和緩存結果的行
function query_unbuffered($sql=""){
return $this->query($sql, 'MySQL_unbuffered_query');
}
//從結果集中取得一行作為關聯數組
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;
}
//取得結果集中行的數目,僅對 INSERT,UPDATE 或者 DELETE
function affected_rows() {
return @MySQL_affected_rows($this->connection_id);
}
//取得結果集中行的數目,僅對 SELECT 語句有效
function num_rows($query_id="") {
if ($query_id == "") $query_id = $this->query_id;
return @MySQL_num_rows($query_id);
}
//返回上一個 MySQL 操作中的錯誤信息的數字編碼
function get_errno(){
$this->errno = @MySQL_errno($this->connection_id);
return $this->errno;
}
//取得上一步 INSERT 操作產生的 ID
function insert_id(){
return @MySQL_insert_id($this->connection_id);
}
//得到查詢次數
function query_count() {
return $this->query_count;
}
//釋放結果內存
function free_result($query_id=""){
if ($query_id == "") $query_id = $this->query_id;
@MySQL_free_result($query_id);
}
//關閉 MySQL 連接
function close_db(){
if ( $this->connection_id ) return @MySQL_close( $this->connection_id );
}
//列出 MySQL 數據庫中的表
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;
}
//從結果集中取得列信息並作為對象返回,取得所有字段
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;
}
//錯誤提示
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 數據庫錯誤</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);
}
/* 根據條件查詢,返回所有記錄
* $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);
}
//詳細顯示一條記錄
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;
}
//計數統計
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"];
}
}
?>
[Ctrl+A 全部選擇 然後拷貝]
很久沒有發帖了,把我常用的一些PHP類文件分享出來。
如果您加了新功能,或者是有改進,請與大家一起分享。
<?PHP
$db_config["hostname"] = "127.0.0.1"; //服務器地址
$db_config["username"] = "root"; //數據庫用戶名
$db_config["passWord"] = "root"; //數據庫密碼
$db_config["database"] = "wap_blueidea_com"; //數據庫名稱
$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');
?>
更詳細的使用方法,請參考 db類文件中的注釋。