親,你的想法有問(wèn)題,這是不符合要求的。

成都創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比象山網(wǎng)站開(kāi)發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式象山網(wǎng)站制作公司更省心,省錢(qián),快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋象山地區(qū)。費(fèi)用合理售后完善,十載實(shí)體公司更值得信賴。
數(shù)據(jù)庫(kù)中有很多的表,表中有字段。因此你不可能查詢數(shù)據(jù)庫(kù)中的id和news,而是只能在特定的表上查詢。同時(shí)sql語(yǔ)法也要求
select
fields
from
table_name而不是db_name哦。
舉例來(lái)說(shuō),保存新聞的表名字是news,
位于數(shù)據(jù)庫(kù)
my_test_db中,那么應(yīng)該
$con = mysql_connect("127.0.0.1", "123", "123");
mysql_select_db('my_test_db', $con);
$sql = "select id, news from news";后面的代碼就一樣了
原生SQL查詢有 query() 和 execute() 兩個(gè)方法:
query():用于 SQL 查詢操作,并返回符合查詢條件的數(shù)據(jù)集
execute():更新和寫(xiě)入數(shù)據(jù)的 SQL 操作,返回影響的記錄數(shù)
query()
query() 方法是用于 SQL 查詢操作,和select()方法一樣返回符合查詢條件的數(shù)據(jù)集。
例子:
public function read(){
// 實(shí)例化一個(gè)空模型,沒(méi)有對(duì)應(yīng)任何數(shù)據(jù)表
$Dao = M();
//或者使用 $Dao = new Model();
$list = $Dao-query("select * from user where uid5");
if($list){
$this-assign('list', $list );
$this-display();
} else {
$this-error($Dao-getError());
}
}
對(duì)于 query() 方法返回的數(shù)據(jù)集,跟 select() 一樣,可以在模板里直接循環(huán)輸出。
execute()
execute() 方法用于更新和寫(xiě)入數(shù)據(jù)的 SQL 操作(注:非查詢操作,無(wú)返回?cái)?shù)據(jù)集),返回影響的記錄數(shù)。
例子:
public function read(){
header("Content-Type:text/html; charset=utf-8");
// 實(shí)例化一個(gè)空模型,沒(méi)有對(duì)應(yīng)任何數(shù)據(jù)表
$Dao = M();
//或者使用 $Dao = new Model();
$num = $Dao-execute("update user set email = '12345@xxx.com' where uid=3");
if($num){
echo '更新 ',$num,' 條記錄。';
}else{
echo '無(wú)記錄更新';
}
}
如果查詢比較復(fù)雜或一些特殊的數(shù)據(jù)操作不能通過(guò) ThinkPHP 內(nèi)置的 ORM 和 ActiveRecord 模式實(shí)現(xiàn)時(shí),就可以通過(guò)直接使用原生 SQL 查詢來(lái)實(shí)現(xiàn)。
注意:以上都是 user 沒(méi)有表前綴的例子,在查詢語(yǔ)句中,查詢的表應(yīng)該寫(xiě)實(shí)際的表名字(包括前綴)。
(我把你表格中的數(shù)據(jù)?"中文"?換成了?"英文"?數(shù)據(jù)和你的是一致的)
我創(chuàng)建你的表格的SQL語(yǔ)句:
create?table?cellphone
(?id?int(3)?zerofill?not?null?auto_increment?primary?key,
title?char(20)?not?null,
number?int?not?null,
type?char(20)?not?null,
time?date?not?null
);
插入數(shù)據(jù)的SQL語(yǔ)句:
insert?into?cellphone?(title,number,type,time)?values
("Phone?Model?1",90,"cellphone","2012-08-14"),
("Phone?Model?1",90,"cellphone","2012-08-14"),
("Phone?Model?2",100,"cellphone","2012-08-14"),
("Mobile?Accessory?1",100,"Accessory","2012-08-14");
查詢語(yǔ)句:
select?title,?sum(number),?time
from?cellphone
where?type?=?'cellphone'
and?time=?date_format(now(),'%Y-%m-%d')?//獲取今天日期的函數(shù)
group?by?title;
創(chuàng)建后的表:
查詢結(jié)果:
php?實(shí)現(xiàn)代碼:
?php
/************************?MySQL連接?************************/
$dbc?=?@?mysqli_connect('localhost',?'root',?'mysql',?'test');
if?(mysqli_connect_errno())?{
? echo?"Error:?Could?not?connect?to?database.Please?try?again?later.";
? exit;
}
/************************************************************/
/**********************************?執(zhí)行查詢?**********************************/
$query?=?"select?title,?sum(number),?time
from?cellphone
where?type?=?'cellphone'
and?time=?date_format(now(),'%Y-%m-%d')
group?by?title";
$results?=?mysqli_query($dbc,?$query);
if?($results)?{
? echo?mysqli_affected_rows($dbc)."?rows?get.br/";
}?else?{
? echo?"An?error?has?occurred.";
}
/***********************************************************************************/
/**********************************?輸出結(jié)果?**********************************/
? while?(?list($title,?$num,?$time)?=?mysqli_fetch_row($results)?)?{
?
? ? ? echo?"$title? ?$num? ?$timebr/";
? ? ?
? }
/******************************************************************************/
/*********************************?關(guān)閉和釋放?*********************************/? ?
? mysqli_free_result($results);
? mysqli_close($dbc);
/******************************************************************************/
?
運(yùn)行結(jié)果:
?php
$host_name="localhost";?//服務(wù)器名
$host_user="root";?//連接服務(wù)器的用戶名
$host_pass="123456";?//連接服務(wù)器的密碼
$db_name="";?//服務(wù)器上的可用數(shù)據(jù)庫(kù)
$my_conn=mysql_connect($host_name,$host_user,$host_pass);?//連接服務(wù)器
mysql_select_db($db_name,$my_conn);?//選擇操作的數(shù)據(jù)庫(kù)
mysql_query("SET?NAMES?utf-s");?//設(shè)置編碼
$sql="select?content?from?sheet?where?id=0?"http://mysql語(yǔ)句
//從sheet表中查詢id=0的content的值
$row?=?mysql_fetch_array(mysql_query($sql,$my_conn));//從mysql返回的結(jié)果中提取一????????????????????????????????????????????????????????????????????????????????????????????//行
?
這是一段典型的使用php連接mysql并查詢數(shù)據(jù)的代碼
本文標(biāo)題:php查詢數(shù)據(jù)字段語(yǔ)句 PHP查詢功能完整代碼實(shí)現(xiàn)
本文URL:http://chinadenli.net/article36/hijssg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、定制網(wǎng)站、微信公眾號(hào)、動(dòng)態(tài)網(wǎng)站、域名注冊(cè)、網(wǎng)站改版
聲明:本網(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í)需注明來(lái)源: 創(chuàng)新互聯(lián)