利用自增字段,把要查詢的數(shù)據表的id設置為自增。查詢數(shù)據時,order by id desc,取第一條就是最新的。

創(chuàng)新互聯(lián)主營南部網站建設的網絡公司,主營網站建設方案,App定制開發(fā),南部h5小程序設計搭建,南部網站營銷推廣歡迎南部等地區(qū)企業(yè)咨詢
利用時間,要查詢的數(shù)據表中增加時間字段,查詢時,order by time desc,取第一條就是最新的。
我直接在這給你修改答案算了
使用的時候刪除行號
修改數(shù)據庫配置
如果想使用
頁面不刷新查詢數(shù)據庫
需要使用JQUERY
如果有需要給我留言
1
?php
2
if(isset($_POST['submit'])$_POST['submit']=='提交'){
3
//判斷是否是提交過來的
4
$intext
=
$_POST['intext'];
5
if($intext!=null||$intext!=''){
6
$link
=
mysql_connect("localhost",
"root",
"123456");
7
//數(shù)據庫配置信息
第一個參數(shù)數(shù)據庫位置第二個是用戶名第三個是密碼
8
mysql_select_db("szn_test");
9
//設置要使用的數(shù)據庫
10
$sql
=
"select
*
from
demo
where
res
=
'".$intext."'";
11
//SQL語句
12
var_dump($sql);
13
$res
=
mysql_query($sql);
14
$arr
=
array();
15
//吧結果存入數(shù)組
并記錄數(shù)組長度
16
$count
=
0;
17
while($data
=
mysql_fetch_array($res)){
18
$arr[$count]
=
$data;
19
$count++;
20
}
21
//關閉數(shù)據庫
22
mysql_close($link);
23
}
24
}
25
26
?
27
html
28
head
29
title/title
30
/head
31
body
32
form
id="form1"
method="post"
action="demo.php"
33
input
type="text"
name="intext"
34
input
type="submit"
name="submit"
value="提交"
35
/form
36
?php
37
if(isset($arr)$arr
!=
null){
38
for($i
=
0;
$i
$count;
$i++){
39
foreach($arr[$i]
as
$key
=
$value){
40
echo
"key:".$key."
value:".$value;
41
echo
"
";
42
}
43
echo
"br";
44
}
45
}
46
?
47
/body
48
/html
這個是數(shù)據庫查詢代碼
你可以看以下對照著修改修改
$sql="select * from mytb order by time desc limit 5"
不足5條,沒關系。
凡事多嘗試。
一.安裝部署mongo
1.創(chuàng)建文件夾?
/opt/mongodb/single?
/opt/mongodb/data/db
2.進入single目錄下載安裝包
//下載 tar.gz文件
wget
3.解壓 并修改文件名?
mv mongodb-linux-x86_64-2.4.6 mongo
4.進入mongo目錄
5.創(chuàng)建配置文件?
– mkdir conf?
– cd conf?
– vim conf.properties
#端口
port=27017
#db存放的目錄
dbpath=/opt/mongotest/data/db
#后臺啟動需要配置日志輸出
logpath=/opt/mongotest/single/mongo/log/mongo.log
#日志模式
logappend=true
#設置成后臺啟動
fork=true
6.啟動mongo?
mongo目錄執(zhí)行
root@chwx2:/opt/mongotest/single/mongo# ./bin/mongod -f conf/conf.properties
about to fork child process, waiting until server is ready for connections.
forked process: 4988
all output going to: /opt/mongotest/single/mongo/log/mongo.log
child process started successfully, parent exiting
表示后臺啟動mongo成功
二.使用mongo進行日常操作
1.連接mongodb?
mongo/bin目錄下執(zhí)行?
./mongo
2.查看數(shù)據庫?
show dbs
3.查看當前所在數(shù)據庫?
db
4.創(chuàng)建數(shù)據庫?
use openfire (臨時創(chuàng)建 如果不做操作 則離開后被系統(tǒng)刪除)
5.在當前數(shù)據庫刪除當前數(shù)據庫?
db.dropDatabase()
6.查看當前庫的所有用戶?
show users
7.查看集合(或者叫表)?
show collections
8.創(chuàng)建集合?
db.createCollection(“mycollection”)
9.創(chuàng)建集合并制定集合的屬性?
db.createCollection(“mycol”, { capped : true, autoIndexID : true, size : 6142800, max : 10000 } )
字段
類型
描述
capped ? ?Boolean ? ?(可選)如果為true,則啟用封頂集合。封頂集合是固定大小的集合,會自動覆蓋最早的條目,當它達到其最大大小。如果指定true,則需要也指定尺寸參數(shù)。 ?
autoIndexID ? ?Boolean ? ?(可選)如果為true,自動創(chuàng)建索引_id字段的默認值是false。 ?
size ? ?number ? ?(可選)指定最大大小字節(jié)封頂集合。如果封頂如果是 true,那么你還需要指定這個字段。 ?
max ? ?number ? ?(可選)指定封頂集合允許在文件的最大數(shù)量。 ?
10.另一種創(chuàng)建集合?
(當插入一條數(shù)據時,不存在mongodb會幫我們創(chuàng)建,和創(chuàng)建數(shù)據庫同理)?
db.MySecondCollection.insert({“name” : “ming”})
11.刪除當前庫的一個集合?
db.CollectionName.drop()
12.插入一條數(shù)據?
db.MyFirstCollection.insert({“_id”:”3”,”title”:”mongotest”,”description”:”this is test”})?
注意:插入的都是JSON形式的,所以一定要用{},否則會報錯:?
Sat Mar 19 14:22:39.160 SyntaxError: Unexpected token :
13.插入一條_id存在的數(shù)據?
db.MyFirstCollection.insert({“_id”:”3”,”title”:”mm”})?
輸出:E11000 duplicate key error index: openfire.MyFirstCollection.$_id_ dup key: { : “3” }?
解釋:_id即是mongodb的默認主鍵,默認自動生成,我們可以直接設置以達到我們想要的目的
問:插入一條和原來集合完全不一樣的數(shù)據可以嗎?
答:可以,所以我們要在程序中規(guī)避,手動指定我們一個集合里面的字段,否則就無法實現(xiàn)查詢及其他功能了
db.MyFirstCollection.insert({"newTitle":"null"})
db.MyFirstCollection.find();
{ "_id" : "3", "title" : "new title", "description" : "this is test" }
{ "_id" : ObjectId("56ecf4fe0dceecace97c4506"), "newTitle" : "null" }
14.查看集合第一條記錄?
db.MySecondCollection.findOne();?
輸出:{ “_id” : ObjectId(“56ecde0662552d15c443dd4a”), “name” : “ming” }
15.查找一條/多條特定數(shù)據?
db.MySecondCollection.findOne({“name”:”ming”})?
輸出:{ “_id” : ObjectId(“56ecde0662552d15c443dd4a”), “name” : “ming” }
16.更新一條記錄?
db.MyFirstCollection.update({“title”:”mongotest”},{$set:{“title”:”new title”}},true,false)?
結果:?
db.MyFirstCollection.findOne()?
{ “_id” : “3”, “title” : “new title”, “description” : “this is test” }
update詳解
db.collection.update(criteria,objNew,upsert,multi)
criteria:查詢條件
objNew:update對象和一些更新操作符
upsert:如果不存在update的記錄,是否插入objNew這個新的文檔,true為插入,默認為false,不插入。
multi:默認是false,只更新找到的第一條記錄。如果為true,把按條件查詢出來的記錄全部更新。
更新操作符:
1.$inc
用法:{$inc:{field:value}}
作用:對一個數(shù)字字段的某個field增加value
示例:db.students.update({name:"student"},{$inc:{age:5}}) ?
2.$set
用法:{$set:{field:value}}
作用:把文檔中某個字段field的值設為value
示例:db.students.update({name:"student"},{$set:{age:23}})
3.$unset
用法:{$unset:{field:1}}
作用:刪除某個字段field
示例: db.students.update({name:"student"},{$unset:{age:1}})
4.$push
用法:{$push:{field:value}}
作用:把value追加到field里。注:field只能是數(shù)組類型,如果field不存在,會自動插入一個數(shù)組類型
示例:db.students.update({name:"student"},{$push:{"title":"major"}}
5.$rename
用法:{$rename:{old_field_name:new_field_name}}
作用:對字段進行重命名(不是值,是字段)
示例:db.students.update({name:"student"},{$rename:{"name":"newname"}})
設置用戶權限及訪問
1.給admin添加用戶
切換到admin庫:use admin?
添加admin用戶:db.addUser(“root”,”admin”)
注意:必須先給admin庫添加用戶,否則就算給具體的庫添加了用戶,后續(xù)進入依然能操作具體庫.?
因為默認admin庫用戶具有最高權限,相當于具體庫需要認證時,進入的用戶持有的是admin的權限,所以無需認證依然能進行操作.
2.給具體庫添加用戶
use openfire
db.addUser("openfire","password")
3.重啟mongo并在啟動時配置需要認證
方法一:退出重啟并加上auth參數(shù)?
./bin/mongod -f conf/conf.properties –auth(注意是兩個-)
方法二:修改配置文件,加上參數(shù)(推薦)?
auth=true
3.測試權限
use openfire
db.MySecondCollection.findOne();
Sat Mar 19 14:04:55.653 error: {
"$err" : "not authorized for query on openfire.MySecondCollection",
"code" : 16550
} at src/mongo/shell/query.js:128
4.認證并操作
//此處應該設置成更加恰當?shù)挠脩裘懊艽a
db.auth("openfire","password")
1
db.MySecondCollection.findOne()
{ "_id" : ObjectId("56ecde0662552d15c443dd4a"), "name" : "ming" }
5.另一種方法認證
在進入mongo時候加上數(shù)據庫及用戶名、密碼?
./bin/mongo openfire -uopenfire -ppassword
root@test:/opt/mongotest/single/mongo# ./bin/mongo openfire -uopenfire -ppassword
MongoDB shell version: 2.4.6
connecting to: openfire
db
openfire
db.MySecondCollection.findOne()
{ "_id" : ObjectId("56ecde0662552d15c443dd4a"), "name" : "ming" }
mongdb進階
1.find()指定返回字段?
作用:減少流量
如果key:value,返回的value是個非常大的JSON,而我們不需要這么多字段,也許我們只需要其中的一個.
那么使用projection來過濾:?
db.collection.find(query, projection) 此處projection就是返回值的過濾條件
參數(shù)
類型
描述
query ? ?文檔 ? ?可選. 使用查詢操作符指定查詢條件 ?
projection ? ?文檔 ? ?可選.使用投影操作符指定返回的鍵。查詢時返回文檔中所有鍵值, 只需省略該參數(shù)即可(默認省略). ?
返回值: 匹配查詢條件的文檔集合的游標. 如果指定投影參數(shù),查詢出的文檔返回指定的鍵 ,”_id”鍵也可以從集合中移除掉。?
_id不指定的話,總是會返回.?
1或者true代表返回,0或者false代表不返回
示例:?
db.MyFirstCollection.findOne({“title”:”new title”},{“description”:1,”_id”:0});?
{ “description” : “this is test” }
也可以使用表達式:?
db.MyFirstCollection.findOne({“title”:”new title”},{Items:{“$slice”:[3,1]}});
引用:?
本文名稱:php查詢最新一條數(shù)據庫 php查詢一條記錄
轉載源于:http://chinadenli.net/article48/doghgep.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供網站建設、App開發(fā)、定制網站、動態(tài)網站、云服務器、網站維護
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)