8.4.4 Model類(lèi)

站在用戶(hù)的角度思考問(wèn)題,與客戶(hù)深入溝通,找到樺甸網(wǎng)站設(shè)計(jì)與樺甸網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶(hù)體驗(yàn)好的作品,建站類(lèi)型包括:成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、國(guó)際域名空間、網(wǎng)站空間、企業(yè)郵箱。業(yè)務(wù)覆蓋樺甸地區(qū)。
getModelName() 獲取當(dāng)前Model的名稱(chēng)
getTableName() 獲取當(dāng)前Model的數(shù)據(jù)表名稱(chēng)
switchModel(type,vars=array()) 動(dòng)態(tài)切換模型
table() 設(shè)置當(dāng)前操作的數(shù)據(jù)表
field() 設(shè)置要查詢(xún)的數(shù)據(jù)字段
where() 設(shè)置查詢(xún)或者操作條件
data(data) 設(shè)置數(shù)據(jù)對(duì)象
order(order) 設(shè)置排序
limit(limit) 查詢(xún)限制
page(page) 查詢(xún)分頁(yè)
join(join) 進(jìn)行JOIN查詢(xún)
having(having) 進(jìn)行having查詢(xún)
group(group) 進(jìn)行g(shù)roup查詢(xún)
lock(lock) 查詢(xún)鎖定
distinct(distinct) 唯一性查詢(xún)
count(field) 記錄統(tǒng)計(jì)
sum(field) 總數(shù)查詢(xún)
min(field) 最小值查詢(xún)
max(field) 最大值查詢(xún)
avg(field) 平均值查詢(xún)
_initialize() 模型初始化方法
_facade(data) 對(duì)保存到數(shù)據(jù)庫(kù)的數(shù)據(jù)進(jìn)行處理
_before_write(data) 寫(xiě)入數(shù)據(jù)前的回調(diào)方法 包括新增和更新
add(data='',options=array()) 新增數(shù)據(jù)
_before_insert(data,options) 寫(xiě)入數(shù)據(jù)前的回調(diào)方法
_after_insert(data,options) 寫(xiě)入數(shù)據(jù)后的回調(diào)方法
selectAdd(fields='',table='',options=array()) 通過(guò)Select方式添加記錄
save(data='',options=array()) 更新數(shù)據(jù)到數(shù)據(jù)庫(kù)
_before_update(data,options) 更新數(shù)據(jù)前的回調(diào)方法
_after_update(data,options) 更新成功后的回調(diào)方法
delete(options=array()) 刪除數(shù)據(jù)
_after_delete(data,options) 刪除成功后的回調(diào)方法
select(options=array()) 查詢(xún)數(shù)據(jù)集
_after_select(resultSet,options) 查詢(xún)成功后的回調(diào)方法
findAll(options=array()) select方法的別名
_options_filter(options) 表達(dá)式過(guò)濾回調(diào)方法
find(options=array()) 查詢(xún)數(shù)據(jù)
_after_find(result,options) 查詢(xún)成功的回調(diào)方法
setField(field,value,condition='') 設(shè)置記錄的某個(gè)字段值
setInc(field,condition='',step=1) 字段值增長(zhǎng)
setDec(field,condition='',step=1) 字段值減少
getField(field,condition='',sepa=' ') 獲取某個(gè)字段值
create(data='',type='') 創(chuàng)建數(shù)據(jù)對(duì)象
autoCheckToken(data) 表單令牌驗(yàn)證
query(sql) 執(zhí)行原生SQL查詢(xún)
execute(sql='') 執(zhí)行原生SQL操作
startTrans() 啟動(dòng)事務(wù)
commit() 提交事務(wù)
rollback() 事務(wù)回滾
getError() 獲取模型的錯(cuò)誤信息
getDbError() 獲取數(shù)據(jù)庫(kù)的錯(cuò)誤信息
getLastInsID() 獲取最后執(zhí)行的SQL語(yǔ)句
getPk() 獲取主鍵名稱(chēng)
getDbFields() 獲取數(shù)據(jù)表的字段信息
regex(value,rule) 使用正則驗(yàn)證數(shù)據(jù)
setProperty(name,value) 設(shè)置模型的屬性值
2.1版新增方法:
db(linkNum,config='') 切換當(dāng)前數(shù)據(jù)庫(kù)連接
高級(jí)模型類(lèi)AdvModel
topN(count,options=array()) 查詢(xún)滿(mǎn)足條件的前N個(gè)記錄
getN(position=0,options=array()) 查詢(xún)符合條件的第N條記錄
0 表示第一條記錄 -1 表示最后一條記錄
first(options=array()) 獲取滿(mǎn)足條件的第一條記錄
last(options=array()) 獲取滿(mǎn)足條件的最后一條記錄
returnResult(data,type='') 返回指定的數(shù)據(jù)類(lèi)型
setLazyInc(field,condition='',step=1,lazyTime=0) 字段值延遲增長(zhǎng)
setLazyDec(field,condition='',step=1,lazyTime=0) 字段值延遲減少
addConnect(config,linkNum=NULL) 增加數(shù)據(jù)庫(kù)連接
delConnect(linkNum) 刪除數(shù)據(jù)庫(kù)連接
closeConnect(linkNum) 關(guān)閉數(shù)據(jù)庫(kù)連接
switchConnect(linkNum,name='') 切換數(shù)據(jù)庫(kù)連接
patchQuery(sql=array()) 批處理執(zhí)行SQL語(yǔ)句
getPartitionTableName(data=array()) 得到分表的的數(shù)據(jù)表名
php 中 mysqli 是個(gè)類(lèi),這個(gè)類(lèi)的函數(shù)(方法)有:
mysqli::$affected_rows — Gets the number of affected rows in a previous MySQL operation
mysqli::autocommit — 打開(kāi)或關(guān)閉本次數(shù)據(jù)庫(kù)連接的自動(dòng)命令提交事務(wù)模式
mysqli::begin_transaction — Starts a transaction
mysqli::change_user — Changes the user of the specified database connection
mysqli::character_set_name — 返回當(dāng)前數(shù)據(jù)庫(kù)連接的默認(rèn)字符編碼
mysqli::$client_info — Get MySQL client info
mysqli::$client_version — Returns the MySQL client version as a string
mysqli::close — 關(guān)閉先前打開(kāi)的數(shù)據(jù)庫(kù)連接
mysqli::commit — 提交一個(gè)事務(wù)
mysqli::$connect_errno — Returns the error code from last connect call
mysqli::$connect_error — Returns a string description of the last connect error
mysqli::__construct — Open a new connection to the MySQL server
mysqli::debug — Performs debugging operations
mysqli::dump_debug_info — 將調(diào)試信息輸出到日志
mysqli::errno — 返回最近函數(shù)調(diào)用的錯(cuò)誤代碼
mysqli::$error_list — Returns a list of errors from the last command executed
mysqli::$error — Returns a string description of the last error
mysqli::$field_count — Returns the number of columns for the most recent query
mysqli::get_charset — Returns a character set object
mysqli::get_client_info — Get MySQL client info
mysqli_get_client_stats — Returns client per-process statistics
mysqli_get_client_version — 作為一個(gè)整數(shù)返回MySQL客戶(hù)端的版本
mysqli::get_connection_stats — Returns statistics about the client connection
mysqli::$host_info — 返回一個(gè)表述使用的連接類(lèi)型的字符串
mysqli::$protocol_version — 返回MySQL使用的協(xié)議版本號(hào)
mysqli::$server_info — 返回MySQL服務(wù)器的版本號(hào)
mysqli::$server_version — 作為一個(gè)整數(shù)返回MySQL服務(wù)器的版本
mysqli::get_warnings — Get result of SHOW WARNINGS
mysqli::$info — Retrieves information about the most recently executed query
mysqli::init — Initializes MySQLi and returns a resource for use with mysqli_real_connect()
mysqli::$insert_id — Returns the auto generated id used in the last query
mysqli::kill — Asks the server to kill a MySQL thread
mysqli::more_results — Check if there are any more query results from a multi query
mysqli::multi_query — Performs a query on the database
mysqli::next_result — Prepare next result from multi_query
mysqli::options — Set options
mysqli::ping — Pings a server connection, or tries to reconnect if the connection has gone down
mysqli::poll — Poll connections
mysqli::prepare — Prepare an SQL statement for execution
mysqli::query — 對(duì)數(shù)據(jù)庫(kù)執(zhí)行一次查詢(xún)
mysqli::real_connect — 建立一個(gè) MySQL 服務(wù)器連接
mysqli::real_escape_string — Escapes special characters in a string for use in an SQL statement, taking into account the current charset of the connection
mysqli::real_query — 執(zhí)行一個(gè)mysql查詢(xún)
mysqli::reap_async_query — Get result from async query
mysqli::refresh — Refreshes
mysqli::release_savepoint — Removes the named savepoint from the set of savepoints of the current transaction
mysqli::rollback — 回退當(dāng)前事務(wù)
mysqli::rpl_query_type — Returns RPL query type
mysqli::savepoint — Set a named transaction savepoint
mysqli::select_db — 選擇用于數(shù)據(jù)庫(kù)查詢(xún)的默認(rèn)數(shù)據(jù)庫(kù)
mysqli::send_query — 發(fā)送請(qǐng)求并返回結(jié)果
mysqli::set_charset — 設(shè)置默認(rèn)字符編碼
mysqli::set_local_infile_default — Unsets user defined handler for load local infile command
mysqli::set_local_infile_handler — Set callback function for LOAD DATA LOCAL INFILE command
mysqli::$sqlstate — Returns the SQLSTATE error from previous MySQL operation
mysqli::ssl_set — Used for establishing secure connections using SSL
mysqli::stat — Gets the current system status
mysqli::stmt_init — 初始化一條語(yǔ)句并返回一個(gè)用于mysqli_stmt_prepare(調(diào)用)的對(duì)象
mysqli::store_result — Transfers a result set from the last query
mysqli::$thread_id — Returns the thread ID for the current connection
mysqli::thread_safe — 返回是否是線(xiàn)程安全的
mysqli::use_result — Initiate a result set retrieval
mysqli::$warning_count — Returns the number of warnings from the last query for the given link
以上函數(shù)清單直接來(lái)自 網(wǎng)站。你可以進(jìn)入該網(wǎng)站參看。
mysql可通過(guò)兩種方式通過(guò)php與web相連,一種通過(guò)php的mysql相關(guān)函數(shù),另一種通過(guò)php的odbc相關(guān)函數(shù)
相關(guān)函數(shù)如下:
mysql函數(shù)
mysql_affected_rows: 得到 mysql 最后操作影響的列數(shù)目。
mysql_close: 關(guān)閉 mysql 伺服器連線(xiàn)。
mysql_connect: 開(kāi)啟 mysql 伺服器連線(xiàn)。
mysql_create_db: 建立一個(gè) mysql 新資料庫(kù)。
mysql_data_seek: 移動(dòng)內(nèi)部傳回指標(biāo)。
mysql_db_query: 送查詢(xún)字串 (query) 到 mysql 資料庫(kù)。
mysql_drop_db: 移除資料庫(kù)。
mysql_errno: 傳回錯(cuò)誤訊息代碼。
mysql_error: 傳回錯(cuò)誤訊息。
mysql_fetch_array: 傳回陣列資料。
mysql_fetch_field: 取得欄位資訊。
mysql_fetch_lengths: 傳回單列各欄資料最大長(zhǎng)度。
mysql_fetch_object: 傳回物件資料。
mysql_fetch_row: 傳回單列的各欄位。
mysql_field_name: 傳回指定欄位的名稱(chēng)。
mysql_field_seek: 設(shè)定指標(biāo)到傳回值的某欄位。
mysql_field_table: 獲得目前欄位的資料表 (table) 名稱(chēng)。
mysql_field_type: 獲得目前欄位的型態(tài)。
mysql_field_flags: 獲得目前欄位的旗標(biāo)。
mysql_field_len: 獲得目前欄位的長(zhǎng)度。
mysql_free_result: 釋放傳回占用記憶體。
mysql_insert_id: 傳回最后一次使用 insert 指令的 id。
mysql_list_fields: 列出指定資料表的欄位 (field)。
mysql_list_dbs: 列出 mysql 伺服器可用的資料庫(kù) (database)。
mysql_list_tables: 列出指定資料庫(kù)的資料表 (table)。
mysql_num_fields: 取得傳回欄位的數(shù)目。
mysql_num_rows: 取得傳回列的數(shù)目。
mysql_pconnect: 開(kāi)啟 mysql 伺服器長(zhǎng)期連線(xiàn)。
mysql_query: 送出一個(gè) query 字串。
mysql_result: 取得查詢(xún) (query) 的結(jié)果。
mysql_select_db: 選擇一個(gè)資料庫(kù)。
mysql_tablename: 取得資料表名稱(chēng)。
odbc函數(shù)
使用odbc函數(shù)需安裝mysql odbc
odbc_autocommit: 開(kāi)關(guān)自動(dòng)更動(dòng)功能。
odbc_binmode: 設(shè)定二進(jìn)位資料處理方式。
odbc_close: 關(guān)閉 odbc 連結(jié)。
odbc_close_all: 關(guān)閉所有 odbc 連結(jié)。
odbc_commit: 更動(dòng) odbc 資料庫(kù)。
odbc_connect: 連結(jié)至 odbc 資料庫(kù)。
odbc_cursor: 取得游標(biāo)名。
odbc_do: 執(zhí)行 sql 指令。
odbc_exec: 執(zhí)行 sql 指令。
odbc_execute: 執(zhí)行預(yù)置 sql 指令。
odbc_fetch_into: 取得傳回的指定列。
odbc_fetch_row: 取得傳回一列。
odbc_field_name: 取得欄位名稱(chēng)。
odbc_field_type: 取得欄位資料形態(tài)。
odbc_field_len: 取得欄位資料長(zhǎng)度。
odbc_free_result: 釋出傳回資料的記憶體。
odbc_longreadlen: 設(shè)定傳回欄的最大值。
odbc_num_fields: 取得欄位數(shù)目。
odbc_pconnect: 長(zhǎng)期連結(jié)至 odbc 資料庫(kù)。
odbc_prepare: 預(yù)置 sql 指令。
odbc_num_rows: 取得傳回列數(shù)目。
odbc_result: 取得傳回資料。
odbc_result_all: 傳回 html 表格資料。
odbc_rollback: 撤消當(dāng)前交易。
odbc_setoption: 調(diào)整 odbc 設(shè)定。
1、PHP獲取顯示數(shù)據(jù)庫(kù)數(shù)據(jù)函數(shù)之 mysql_result()
mixed mysql_result(resource result_set, int row [,mixed field])
從result_set 的指定row 中獲取一個(gè)field 的數(shù)據(jù). 簡(jiǎn)單但是效率低.
舉例:
$link1?=?@mysql_connect("server1",?
"webuser",?"password")?
or?die("Could?not?connect?
to?mysql?server!");
@mysql_select_db("company")?
or?die("Could?not?select?database!");
$query?=?"select?id,?name?
from?product?order?by?name";?
$result?=?mysql_query($query);
$id?=?mysql_result($result,?0,?"id");
$name?=?mysql_result($result,?0,?"name");
mysql_close();
注意,上述代碼只是輸出結(jié)果集中的第一條數(shù)據(jù)的字段值,如果要輸出所有記錄,需要循環(huán)處理.
for?($i?=?0;?$i?=?mysql_num_rows($result);?$i++)
{
$id?=?mysql_result($result,?0,?"id");
$name?=?mysql_result($result,?0,?"name");
echo?"Product:?$name?($id)";
}
注意,如果查詢(xún)字段名是別名,則mysql_result中就使用別名.
2、PHP獲取顯示數(shù)據(jù)庫(kù)數(shù)據(jù)函數(shù)之mysql_fetch_row()
array mysql_fetch_row(resource result_set)
從result_set中獲取整行,把數(shù)據(jù)放入數(shù)組中.
舉例(注意和list 的巧妙配合):
$query?=?"select?id,?
name?from?product?order?by?name";?
$result?=?mysql_query($query);
while(list($id,?$name)?
=?mysql_fetch_row($result))?{
echo?"Product:?$name?($id)";
}
3、PHP獲取顯示數(shù)據(jù)庫(kù)數(shù)據(jù)函數(shù)之mysql_fetch_array()
array mysql_fetch_array(resource result_set [,int result_type])
mysql_fetch_row()的增強(qiáng)版.
將result_set的每一行獲取為一個(gè)關(guān)聯(lián)數(shù)組或/和數(shù)值索引數(shù)組.
默認(rèn)獲取兩種數(shù)組,result_type可以設(shè)置:
MYSQL_ASSOC:返回關(guān)聯(lián)數(shù)組,字段名=字段值?
MYSQL_NUM:返回?cái)?shù)值索引數(shù)組.
MYSQL_BOTH:獲取兩種數(shù)組.因此每個(gè)字段可以按索引偏移引用,也可以按字段名引用.
舉例:
$query?=?"select?id,
name?from?product?order?by?name";
$result?=?mysql_query($query);
while($row?=?mysql_fetch_array
($result,?MYSQL_BOTH))?{?
$name?=?$row['name'];
//或者?$name?=?$row[1];
$name?=?$row['id'];
//或者?$name?=?$row[0];
echo?"Product:?$name?($id)";
}
4、PHP獲取顯示數(shù)據(jù)庫(kù)數(shù)據(jù)函數(shù)之mysql_fetch_assoc()
array mysql_fetch_assoc(resource result_set)
相當(dāng)于 mysql_fetch_array($result, MYSQL_ASSOC)
5、PHP獲取顯示數(shù)據(jù)庫(kù)數(shù)據(jù)函數(shù)之mysql_fetch_object()
object mysql_fetch_object(resource result_set)?
和mysql_fetch_array()功能一樣,不過(guò)返回的不是數(shù)組,而是一個(gè)對(duì)象.
舉例:
$query?=?"select?id,?name?
from?product?order?by?name";
$result?=?mysql_query($query);?
while($row?=?mysql_fetch_object
($result))?{
$name?=?$row-name;
$name?=?$row-id;
echo?"Product:?$name?($id)";
}
以上這些函數(shù)就是PHP獲取顯示數(shù)據(jù)庫(kù)數(shù)據(jù)函數(shù)的全部總結(jié)。
當(dāng)前文章:php發(fā)送數(shù)據(jù)庫(kù)的函數(shù) php發(fā)送數(shù)據(jù)庫(kù)的函數(shù)有哪些
本文路徑:http://chinadenli.net/article4/ddojgoe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、、軟件開(kāi)發(fā)、外貿(mào)網(wǎng)站建設(shè)、服務(wù)器托管、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)