008. 數(shù)據(jù)庫的數(shù)據(jù)查詢
創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供五峰網(wǎng)站建設(shè)、五峰做網(wǎng)站、五峰網(wǎng)站設(shè)計(jì)、五峰網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、五峰企業(yè)網(wǎng)站模板建站服務(wù),十年五峰做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
本節(jié)課我們來了解一下數(shù)據(jù)庫的數(shù)據(jù)查詢方式,單數(shù)據(jù)、數(shù)據(jù)集和其它查詢。
一.單數(shù)據(jù)查詢
1. Db::table()中table必須指定完整數(shù)據(jù)表(包括前綴);
2. 如果希望只查詢一條數(shù)據(jù),可以使用find()方法,需指定where條件;
Db::table('tp_user')-where('id', 27)-find()
3. Db::getLastSql()方法,可以得到最近一條SQL查詢的原生語句;
SELECT * FROM `tp_user` LIMIT 1
4. 沒有查詢到任何值,則返回null;
5. 使用findOrFail()方法同樣可以查詢一條數(shù)據(jù),在沒有數(shù)據(jù)時(shí)拋出一個(gè)異常;
Db::table('tp_user')-where('id', 1)-findOrFail()
6. 使用findOrEmpty()方法也可以查詢一條數(shù)據(jù),但在沒有數(shù)據(jù)時(shí)返回一個(gè)空數(shù)組;
7. Db::table('tp_user')-where('id', 1)-findOrEmpty();
二.數(shù)據(jù)集查詢
1. 想要獲取多列數(shù)據(jù),可以使用select()方法;
Db::table('tp_user')-select(); SELECT * FROM `tp_user`
2. 多列數(shù)據(jù)在查詢不到任何數(shù)據(jù)時(shí)返回空數(shù)組,使用selectOrFail()拋出異常; Db::table('tp_user')-where('id', 1)-selectOrFail();
3. 在select()方法后再使用toArray()方法,可以將數(shù)據(jù)集對(duì)象轉(zhuǎn)化為數(shù)組;
4. 當(dāng)在數(shù)據(jù)庫配置文件中設(shè)置了前綴,那么我們可以使用name()方法忽略前綴; Db::name('user')-select();
三.其它查詢
1. 通過value()方法,可以查詢指定字段的值(單個(gè)),沒有數(shù)據(jù)返回null;
Db::name('user')-where('id', 27)-value('username');
$user = Db::table('tp_user')-select()-toArray(); dump($user);
2. 通過colunm()方法,可以查詢指定列的值(多個(gè)),沒有數(shù)據(jù)返回空數(shù)組; Db::name('user')-column('username');
3. 可以指定id作為列值的索引;
4. 如果處理的數(shù)據(jù)量巨大,成百上千那種,一次性讀取有可能會(huì)導(dǎo)致內(nèi)存開銷過大;
5. 為了避免內(nèi)存處理太多數(shù)據(jù)出錯(cuò),可以使用chunk()方法分批處理數(shù)據(jù);
6. 比如,每次只處理100條,處理完畢后,再讀取100條繼續(xù)處理;
7. 可以利用游標(biāo)查詢功能,可以大幅度減少海量數(shù)據(jù)的內(nèi)存開銷,它利用了PHP生成器特性。每次查詢只讀一行,然后再讀取時(shí),自動(dòng)定位到下一行繼續(xù)讀取;
Db::name('user')-column('username', 'id');
Db::table('tp_user')-chunk(3, function($users) { foreach ($users as $user) {
dump($user);
}
echo 1; });
$cursor = Db::table('tp_user')-cursor(); foreach($cursor as $user){
dump($user);
}
在使用php session進(jìn)行數(shù)據(jù)查詢時(shí)主要有3中方法。第一種是使用函數(shù)【mysql_connect()】建立和MYSQL數(shù)據(jù)庫的連接。mysql_connect()是用來建立和MYSQL數(shù)據(jù)庫的連接,一共有5個(gè)參數(shù),一般情況下只使用前3個(gè)參數(shù),分別是MySQL服務(wù)器地址、用戶名以及密碼。第二種是使用函數(shù)【mysql_select_db()】指定要操作的數(shù)據(jù)庫。mysql_select_db()是用來指定要操作的數(shù)據(jù)庫。要是需要操作的數(shù)據(jù)庫還沒有創(chuàng)建,則需要?jiǎng)?chuàng)建數(shù)據(jù)庫,然后再創(chuàng)建數(shù)據(jù)庫中的表。第三種是使用函數(shù)【mysql_query()】查詢指令。mysql_query()是查詢指令的專用函數(shù),所有的SQL語句都通過它執(zhí)行,并返回結(jié)果集。一般情況下這三種方法是最適用的,希望可以解決你的問題。
1、用navicat新建一個(gè)數(shù)據(jù)庫database1。
2、在database1數(shù)據(jù)庫中新建一個(gè)表table2。
3、在table2中添加新的數(shù)據(jù),新建一個(gè)名稱為mysql_query的數(shù)據(jù)庫。
4、在頁面中用mysql_connect 函數(shù)與數(shù)據(jù)庫建立連接。
5、用mysql_select_db函數(shù)選擇要查詢的數(shù)據(jù)庫。
6、添加一個(gè)查詢 table2表的查詢語句“$sql=select * from table2“。
7、將查詢語句$sql添加到查詢數(shù)據(jù)庫函數(shù)mysql_query中,返回值賦值給變量query。
8、最后將mysql_query。php文件在瀏覽器中打開,查看查詢到數(shù)據(jù)庫中的內(nèi)容的結(jié)果。
?php
if(!defined("INCLUDE_MYSQL_OK")) {
define("INCLUDE_MYSQL_OK","");
class MySQL_class {
var $debug = true;
var $db,
$id,
$result, /* 查詢結(jié)果指針 */
$rows, /* 查詢結(jié)果行數(shù) */
$fields, /* 查詢結(jié)果列數(shù) */
$data, /* 數(shù)據(jù)結(jié)果 */
$arows, /* 發(fā)生作用的紀(jì)錄行數(shù)目 */
$iid; /* 上次插入操作后,可能存在的"AUTO_INCREMENT"屬性字段的值,如果為"0",則為空 */
var $user, $pass, $host, $charset;
/*
* 請(qǐng)注意用戶名和密碼是否正確
*/
function Setup ($host, $user, $pass, $charset='utf8') {
$this-host = $host;
$this-user = $user;
$this-pass = $pass;
$this-charset = $charset;
}
function Connect ($db = "") {
global $CFG_MYSQL_INFO;
if (!$this-host) {
$this-host = $CFG_MYSQL_INFO["host"];
}
if (!$this-user) {
$this-user = $CFG_MYSQL_INFO["user"]; /* 在這里作修改 */
}
if (!$this-pass) {
$this-pass = $CFG_MYSQL_INFO["passwd"]; /* 在這里作修改 */
}
if (!$this-charset) {
$this-charset = "utf8"; /* 在這里作修改 */
}
if (empty($db))
$this-db = $CFG_MYSQL_INFO["database"];
else
$this-db = $db;
$this-id = @mysql_connect($this-host, $this-user, $this-pass);
if (!$this-id)
return false;
$this-SelectDB($this-db); /* 定位到指定數(shù)據(jù)庫 */
$this-Query("SET NAMES '".$this-charset."'");
return true;
}
function Close(){
@mysql_close($this-id);
}
function SelectDB ($db) {
if(!@mysql_select_db($db, $this-id))
return false;
else
return true;
}
function Begin () {
$this-result = @mysql_query("START TRANSACTION WITH CONSISTENT SNAPSHOT", $this-id);
if (!$this-result)
return false;
return true;
}
function Commit () {
$this-result = @mysql_query("COMMIT", $this-id);
if (!$this-result)
return false;
return true;
}
function Rollback () {
$this-result = @mysql_query("ROLLBACK", $this-id);
if (!$this-result)
return false;
return true;
}
function Escape ($str) {
$escstr = mysql_escape_string($str);
return $escstr;
}
# 普通查詢功能,主要用于返回結(jié)果是多條記錄的情況
# 請(qǐng)使用 Fetch 方法取得每條記錄信息
function Query ($query) {
$this-result = @mysql_query($query, $this-id);
if (!$this-result)
{
if ($this-debug)
MySQL_ErrorMsg ("不能執(zhí)行查詢(query): $query");
else
return false;
}
$this-rows = @mysql_num_rows($this-result);
$this-fields = @mysql_num_fields($this-result);
if (!$this-rows) return false;
return true;
}
function QuerySql ($query) {
$ret = @mysql_query($query, $this-id);
if ($ret === false)
{
if ($this-debug)
MySQL_ErrorMsg ("不能執(zhí)行查詢(query): $query");
else
return false;
}
$this-result = $ret;
$this-rows = @mysql_num_rows($this-result);
$this-fields = @mysql_num_fields($this-result);
return true;
}
# 如果查詢結(jié)果為單條記錄時(shí)使用,返回結(jié)果存儲(chǔ)于數(shù)組 data 中
function QueryRow ($query) {
$this-result = @mysql_query($query, $this-id);
if (!$this-result)
{
if ($this-debug)
MySQL_ErrorMsg ("不能執(zhí)行查詢(query): $query");
else
return false;
}
$this-rows = @mysql_num_rows($this-result);
$this-data = @mysql_fetch_array($this-result, MYSQL_ASSOC);
//MySQL_ErrorMsg ("不能從查詢結(jié)果中取得數(shù)據(jù) $query");
if (!$this-result || !$this-rows)
return false;
return true;
}
# 移動(dòng)到指定記錄行,將該行結(jié)果儲(chǔ)存于數(shù)組 data 中
function Fetch ($row) {
if(!@mysql_data_seek($this-result, $row))
//MySQL_ErrorMsg ("不能定位到指定數(shù)據(jù)行 $row");
return false;
$this-data = @mysql_fetch_array($this-result, MYSQL_ASSOC);
//MySQL_ErrorMsg ("不能提取指定數(shù)據(jù)行數(shù)據(jù) $row");
if (!$this-data)
return false;
return true;
}
/* 以下方法將作用于 arows */
/* 此方法將作用于 iid */
function Insert ($query) {
$this-result = @mysql_query($query, $this-id);
if (!$this-result)
{
if ($this-debug)
MySQL_ErrorMsg ("不能執(zhí)行查詢(query): $query");
else
return false;
}
$this-arows = @mysql_affected_rows($this-id);
$this-iid = @mysql_insert_id($this-id);
return true;
}
function Update ($query) {
$this-result = @mysql_query($query, $this-id);
if (!$this-result)
{
if ($this-debug)
MySQL_ErrorMsg ("不能執(zhí)行查詢(query): $query");
else
return false;
}
$this-arows = @mysql_affected_rows($this-id);
if (!$this-arows || $this-arows == -1)
return false;
return true;
}
function Delete ($query) {
$this-result = @mysql_query($query, $this-id);
if (!$this-result)
{
if ($this-debug)
MySQL_ErrorMsg ("不能執(zhí)行查詢(query): $query");
else
return false;
}
$this-arows = @mysql_affected_rows($this-id);
return true;
}
function Error (){
return mysql_error()."(".mysql_errno().")";
}
function Errno (){
return mysql_errno();
}
}
/*
* MySQL_ErrorMsg
* 輸出錯(cuò)誤信息
*/
function MySQL_ErrorMsg ($msg) {
# 關(guān)閉可能影響字符顯示的HTML代碼
echo("/ul/dl/ol\n");
echo("/table/script\n");
# 錯(cuò)誤信息
$text = "font color=\"#000000\" style=\"font-size: 9pt; line-height: 12pt\"p系統(tǒng)提示:".$msg."br";
$text .= "錯(cuò)誤信息:";
$text .= mysql_error()."br";
$text .= "錯(cuò)誤代碼:".mysql_errno()."brbr";
$text .= "請(qǐng)稍候再試,如果問題仍然存在,請(qǐng)與 a href=\"mailto:wuqiong@igenus.org\"系統(tǒng)管理員/a 聯(lián)系!";
$text .= "/font\n";
die($text);
}
}
?
一些細(xì)節(jié)的地方自己修改吧 主要是我在別的文件專門定義了全局變量,你看一遍,把應(yīng)改的地方改一下就好了
標(biāo)題名稱:php類查詢數(shù)據(jù)庫 php數(shù)據(jù)庫查詢結(jié)果處理
本文網(wǎng)址:http://muchs.cn/article28/doccpjp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計(jì)公司、全網(wǎng)營(yíng)銷推廣、外貿(mào)建站、網(wǎng)站策劃、微信小程序、App開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)