mysqli有兩種數(shù)據(jù)庫連接方式:
創(chuàng)新互聯(lián)公司成立于2013年,先為織金等服務建站,織金等地企業(yè),進行企業(yè)商務咨詢服務。為織金企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務解決您的所有建站問題。
1、面向過程式連接:
mysqli_connect('localhost','xxx','xxx','xxx');
mysqli_query('');
后使用mysqli_fetch_assoc方法獲取到數(shù)據(jù)。
2、面向?qū)ο笫竭B接:
$mysqli?=?new?mysqli("localhost",?"my_user",?"my_password",?"world");
$result?=?$mysqli-query('');
后使用$result-fetch_assoc()獲取數(shù)據(jù)。
至于num_rows是獲取查詢到的行數(shù)的方法。
//當天時間
$where['time']?=?array(
array('egt',strtotime(date('Y-m-d',time())),
array('lt',strtotime(date('Y-m-d',time())).'+1?day')
);
//?本周時間
$where['time']?=?array(
array('egt',strtotime(date('Y-m-d',time())).'-'.date('w',time()).'?day'),
array('lt',strtotime(date('Y-m-d',time())).'+1?week?-'.date('w',time()).'?day');
);
//?本月時間
$where['time']?=?array(
array('egt',strtotime(date('Y-m',time()))),
array('lt',strtotime(date('Y-m',time()).'+1?month'))
);
//?本年時間
$where['time']?=?array(
array('egt',strtotime(date('Y',time()))),
array('lt',strtotime(date('Y',time()).'+1?year'))
);
上面是查詢條件,直接運用到查詢語句就可以了
$result?=?$db-where($where)-select();
更正下上面的那個?本年?查詢時間
$where['time']?=?array(
array('egt',strtotime(date('Y-01-01',time())),
array('lt',strtotime(date('Y-01-01',time()).'+1?year'))
);
下面是時間戳查詢。如果數(shù)據(jù)庫時間顯示的是 2011-04-05 那就不需要 用 strtotime 時間戳轉(zhuǎn)換函數(shù):
$timea = strtotime($_POST['timea']);
$timeb = strtotime($_POST['timeb']);
$sq2="select * from `ecs_order_info` where add_time between '$timea' and '$timeb' and `quanxian`='$dangqian' order by `order_id` DESC limit 50";
$sql = mysql_query($sq2);
擴展資料
在php中完成
1、UNIX時間戳轉(zhuǎn)換為日期用函數(shù): date()
一般形式:date('Y-m-d H:i:s', 1156219870);
2、日期轉(zhuǎn)換為UNIX時間戳用函數(shù):strtotime()
一般形式:strtotime('2010-03-24 08:15:42');
在MySQL中完成
這種方式在MySQL查詢語句中轉(zhuǎn)換,優(yōu)點是不占用PHP解析器的解析時間,速度快,缺點是只能用在數(shù)據(jù)庫查詢中,有局限性。
1、UNIX時間戳轉(zhuǎn)換為日期用函數(shù): FROM_UNIXTIME()
一般形式:select FROM_UNIXTIME(1156219870);
2、日期轉(zhuǎn)換為UNIX時間戳用函數(shù): UNIX_TIMESTAMP()
一般形式:Select UNIX_TIMESTAMP('2006-11-04 12:23:00′);
舉例:mysql查詢當天的記錄數(shù):
$sql=”select * from message Where DATE_FORMAT(FROM_UNIXTIME(chattime),'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d') order by id desc”。
/*今天*/
select?*?from?表名?where?to_days(時間字段)?=?to_days(now());
/*昨天*/
select?*?from?表名?where?to_days(now())-to_days(時間字段)?=?1;
/*近7天*/
select?*?from?表名?where?date_sub(curdate(),?interval?7?day)?=?date(時間字段);
/*查詢距離當前現(xiàn)在6個月的數(shù)據(jù)*/
select?*?from?表名?where?時間字段?between?date_sub(now(),interval?6?month)?and?now();
/*查詢當前這周的數(shù)據(jù)*/
select?*?from?表名?where?yearweek(date_format(時間字段,'%Y-%m-%d'))?=?yearweek(now());
/*查詢上周的數(shù)據(jù)*/
select?*?from?表名?where?yearweek(date_format(時間字段,'%Y-%m-%d'))?=?yearweek(now())-1;
/*查詢當前月份的數(shù)據(jù)*/
select?*?from?表名?where?date_format(時間字段,'%Y-%m')=date_format(now(),'%Y-%m');
/*查詢上個月的數(shù)據(jù)*/
select?*?from?表名?where?date_format(時間字段,'%Y-%m')=date_format(date_sub(curdate(),?interval?1?month),'%Y-%m');
其它獲取類似以上的代碼顯示
PHP查詢到的數(shù)據(jù)存放到數(shù)組里面,一般使用$arr[]=$row的方式實現(xiàn),$row是mysql_fetch_array獲得的一行數(shù)據(jù),本身是一個數(shù)組,執(zhí)行上面的語句之后,這一行會添加存放在額為數(shù)組$arr的最后。 典型的例子代碼是這樣的:mysql_connect('127.0.0.1', 'root', '123456');$sql='select * from test.tab';if ($res=mysql_query($sql)){ while($row=mysql_fetch_array($res)) $result[]=$row; mysql_free_resule($res);}else echo "執(zhí)行SQL語句:$sql\n錯誤:".mysql_error();echo '查詢結(jié)果在下面的額為數(shù)組里面:';print_r($result);echo '';
比如查2012年11月到12月
select *
from test_table
where to_char(test_date,'yyyy-mm') = '2012-12'
這樣
/********/
哦,那你可以這么做。先在網(wǎng)頁上加兩個下拉列表選擇框,第一個用來獲取起始日期,第二個用來獲得結(jié)束日期。 sql改為 SELECT
TO_CHAR(test_date, 'yyyy-mm') FROM
test_table WHERE
TO_CHAR(test_date, 'yyyy-mm') BETWEEN '2012-11' and '2012-12'
網(wǎng)站題目:php查詢一年之前的數(shù)據(jù),php獲取年份
文章出自:http://chinadenli.net/article42/hesehc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務器托管、網(wǎng)站營銷、App設(shè)計、虛擬主機、網(wǎng)站設(shè)計、域名注冊
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)