日韩天天综合网_野战两个奶头被亲到高潮_亚洲日韩欧美精品综合_av女人天堂污污污_视频一区**字幕无弹窗_国产亚洲欧美小视频_国内性爱精品在线免费视频_国产一级电影在线播放_日韩欧美内地福利_亚洲一二三不卡片区

解析用PHP操作MySql數(shù)據(jù)庫(kù)(4)_PHP教程

編輯Tag賺U幣
教程Tag:暫無(wú)Tag,歡迎添加,賺取U幣!

/*
* 最常用的分頁(yè)方法,只需要傳3個(gè)參數(shù)
* tablename 表名, where 查詢條件, orderby 排序字段(默認(rèn)以id倒序排列)
*/
function get_rows(tablename, where="", orderby=""){
this->table["tablename"] = tablename;
this->table["where"] = where;
orderby ? this->table["orderby"] = orderby : "";
arr = array(
"page" => this->show_page(), //分頁(yè)代碼
"rows" => this->get_rows_by_sql(), //記錄數(shù)
"sum" => this->total_records, //總記錄數(shù)
);
return arr;
}
/*
* 特殊查詢,sql_query 查詢sql語(yǔ)句, row_count 統(tǒng)計(jì)總數(shù)
*/
function get_rows_sql(sql_query, row_count=0) {
this->total_records = row_count;
arr["rows"] = this->get_rows_by_sql(sql_query);
arr["page"] = this->show_page();
arr["sum"] = this->total_records;
return arr;
}
function get_sql(){
if (this->total_records>10000) {
this->sql = "SELECT ".this->table["fileds"]." FROM `".this->table["tablename"]."` ".(this->table["where"]!=""?" WHERE ".this->table["where"].’ AND ’.this->table["id"].’>=’:’ WHERE ’.this->table["id"].’>=’).’(SELECT ’.this->table["id"].’ FROM `’.this->table["tablename"].’` ORDER BY ’.this->table["id"].’ LIMIT ’.this->page_size*(this->page-1).’, 1)’." ORDER BY ".this->table["orderby"]." ".this->table["descasc"]." LIMIT ".this->page_size;
}else{
this->sql = "SELECT ".this->table["fileds"]." FROM `".this->table["tablename"]."` ".(this->table["where"]!=""?" WHERE ".this->table["where"]:"")." ORDER BY ".this->table["orderby"]." ".this->table["descasc"]." LIMIT ".this->page_size*(this->page-1).", ".this->page_size;
}
//SELECT * FROM articles ORDER BY id DESC LIMIT 0, 20
//SELECT * FROM articles WHERE category_id = 123 AND id >= (SELECT id FROM articles ORDER BY id LIMIT 10000, 1) LIMIT 10
return this->sql; //SQL語(yǔ)句
}
function set_url(){
arr_url = array();
parse_str(_SERVER["QUERY_STRING"], arr_url);
unset(arr_url["page"]);
if (empty(arr_url)){
str = "page=";
}else{
str = http_build_query(arr_url)."&page=";
}
this->url = "http://"._SERVER["HTTP_HOST"]._SERVER["PHP_SELF"]."?".str;
}
}
?>

如果您加了新功能,或者是有改進(jìn),請(qǐng)與大家一起分享。

測(cè)試代碼如下,db.php 請(qǐng)到這里下載:

http://dwww.cn/news/2008-6/2008662043517349.shtml

<?php
db_config["hostname"] = "127.0.0.1"; //服務(wù)器地址
db_config["username"] = "root"; //數(shù)據(jù)庫(kù)用戶名
db_config["password"] = "root"; //數(shù)據(jù)庫(kù)密碼
db_config["database"] = "wap_blueidea_com"; //數(shù)據(jù)庫(kù)名稱
db_config["charset"] = "utf8";
config["charset"] = "utf-8"; //網(wǎng)站編碼

include(’db.php’);
include(’pagelist.php’);
db = new db();
db->connect(db_config);
header("content-type:text/html;charset=".config["charset"]);//設(shè)置頁(yè)面編碼
pl = new pagelist();
arr = pl->get_rows(’table_name’);
unset(pl);
echo ’<pre style="text-align:left">’;
print_r(arr);
echo ’</pre>’;
//指定特殊 sql 時(shí)候
pl = new pagelist();
sql = ’SELECT * FROM `wap_article` AS a, `wap_article_info` AS b WHERE a.id=b.articleid’;
arr = pl->get_rows_sql(sql);
unset(pl);
echo ’<pre style="text-align:left">’;
print_r(arr);
echo ’</pre>’;
?>

當(dāng)表中的記錄總數(shù)在 10000條以上時(shí),使用了 子查詢分頁(yè),這樣效率會(huì)更高一些,數(shù)據(jù)量小的時(shí)候,直接查詢更快。這個(gè)是老王說(shuō)的,參考這里:
http://hi.baidu.com/thinkinginlamp/blog/item/c5dea0ecdfef5e392697910f.html

共4頁(yè)上一頁(yè)1234下一頁(yè)
來(lái)源:模板無(wú)憂//所屬分類:PHP教程/更新時(shí)間:2010-02-23
相關(guān)PHP教程