結(jié)
展望
前端代碼
數(shù)據(jù)庫(kù)表結(jié)構(gòu)
服務(wù)器端代碼
顯示非重復(fù)性的數(shù)據(jù)
優(yōu)化顯示
加上滾動(dòng)條
每次都顯示最新消息
消息顯示
消息發(fā)送
消息顯示區(qū)
發(fā)消息
思路
板塊
優(yōu)化
完整代碼
總結(jié)與展望
為更好的運(yùn)用這兩天學(xué)到的Ajax的相關(guān)的知識(shí),就做了個(gè)簡(jiǎn)單的在線(xiàn)網(wǎng)絡(luò)聊天室。
為更好的運(yùn)用這兩天學(xué)到的Ajax的相關(guān)的知識(shí),就做了個(gè)簡(jiǎn)單的在線(xiàn)網(wǎng)絡(luò)聊天室。
思路
實(shí)現(xiàn)聊天室,基本上就是通過(guò)Ajax來(lái)傳遞數(shù)據(jù),讓PHP來(lái)實(shí)現(xiàn)對(duì)數(shù)據(jù)的差入和查找,再交給前端JavaScript實(shí)現(xiàn)頁(yè)面的更新,達(dá)到即時(shí)聊天的功能。
消息顯示區(qū)
消息顯示區(qū)就是一個(gè)DIV塊,我們會(huì)借助Ajax獲取到服務(wù)器端信息之后,使用JavaScript來(lái)更新頁(yè)面。
<h3>消息顯示區(qū)</h3><div id="up"></div><hr />1234
發(fā)消息
發(fā)消息模塊,其實(shí)說(shuō)白了,就是向服務(wù)器上插入數(shù)據(jù)的過(guò)程,也是屬于比較簡(jiǎn)單的。
<h3>發(fā)言欄</h3> <div id="bottom"> <form action="./chatroom_insert.php"> <div id="chat_up"> <span>顏色</span> <input type="color" name="color"/> <span>表情</span> <select name="biaoqing"> <option value="微笑地">微笑地</option> <option value="猥瑣地">猥瑣地</option> <option value="和藹地">和藹地</option> <option value="目不轉(zhuǎn)睛地">目不轉(zhuǎn)睛地</option> <option value="傻傻地">傻傻地</option> </select> <span>聊天對(duì)象</span> <select name="receiver"> <option value="">所有的人</option> <option value="老郭">老郭</option> <option value="小郭">小郭</option> <option value="大郭">大郭</option> </select> </div> <div id="chat_bottom"> <textarea id="msg" name="msg" style="width:380px;height:auto;"></textarea> <input type="button" value="發(fā)言" onclick="send()" /> 發(fā)言:<span id="result"></span> </div> </form> </div>1234567891011121314151617181920212223242526272829
板塊
下面開(kāi)始使用代碼來(lái)實(shí)現(xiàn)相關(guān)的業(yè)務(wù)邏輯。
消息顯示
我們的思路就是每隔一段時(shí)間,客戶(hù)端向服務(wù)器發(fā)送請(qǐng)求,輪詢(xún)獲得最新的數(shù)據(jù)。
<script>function showmessage(){ var ajax=new XMLHttpRequest(); // 從服務(wù)器獲取并處理數(shù)據(jù) ajax.onreadystatechange=function(){ if(ajax.readyState==4) { //alert(ajax.responseText); // 將獲取到的字符串轉(zhuǎn)換成實(shí)體 eval('var data='+ajax.responseText); // 遍歷data數(shù)組,把內(nèi)部的信息一個(gè)個(gè)的顯示到頁(yè)面上 var s=""; for(var i=0 ; i < data.length;i++){ data[i]; s +="("+data[i].add_time+") >>>"; s +="<p style='color:"+data[i].color+";'>"; s +=data[i].sender +" 對(duì) " + data[i].receiver +" "+ data[i].biaoqing+"說(shuō):" + data[i].msg; s +="</p>"; } // 開(kāi)始向頁(yè)面時(shí)追加信息 var showmessage=document.getElementById("up"); showmessage.innerHTML +=s; } } ajax.open('get','./chatroom.php'); ajax.send(null); }// 更新信息的執(zhí)行時(shí)機(jī)window.onload=function(){ //showmessage(); // 制作輪詢(xún),實(shí)現(xiàn)自動(dòng)的頁(yè)面更新 setInterval("showmessage()",3000); }</script>1234567891011121314151617181920212223242526272829303132333435363738
里賣(mài)弄比較重要的就是setInterval函數(shù)的使用,以此來(lái)實(shí)現(xiàn)間隔性的觸發(fā)請(qǐng)求事件。
消息發(fā)送
關(guān)于消息發(fā)送,通過(guò)表單形式發(fā)給服務(wù)器即可。我們這里使用了目前Html5的一個(gè)最新的技術(shù),F(xiàn)ormData,一般來(lái)說(shuō)目前的主流的現(xiàn)代瀏覽器都是支持這一技術(shù)了。使用FormData我們可以方便的獲取一個(gè)表單的數(shù)據(jù)。
注意: FormData收集表單數(shù)據(jù)的時(shí)候是以鍵值對(duì)的形式搜集的,所以對(duì)應(yīng)的表單項(xiàng)一定要有name屬性,否則表單將收集不到該項(xiàng)的數(shù)據(jù)值。
<script> function send(){ // 向服務(wù)器差入相關(guān)的數(shù)據(jù) var form=document.getElementsByTagName('form')[0]; var formdata=new FormData(form); var xhr=new XMLHttpRequest(); xhr.onreadystatechange=function(){ if(xhr.readyState==4) { //alert(xhr.resposneText); document.getElementById("result").innerHTML=xhr.responseText; setTimeout("hideresult()",2000); } } xhr.open('post','./chatroom_insert.php'); xhr.send(formdata); document.getElementById("msg").value=""; //return false; } // 2秒后實(shí)現(xiàn)提示信息的消失 function hideresult(){ document.getElementById('result').innerHTML=""; }</script>123456789101112131415161718192021222324
值得深思的是:setTimeout函數(shù)實(shí)現(xiàn)的功能。在得到服務(wù)器端的反饋信息之后,及時(shí)的更新到發(fā)送按鈕后面,給用戶(hù)一個(gè)很好的體驗(yàn)。
優(yōu)化
做完這里基本上就可以實(shí)現(xiàn)聊天了。但是實(shí)現(xiàn)的效果會(huì)非常的不好,主要是有以下幾點(diǎn)。
沒(méi)有滾動(dòng)顯示,每次都得手動(dòng)的查看最新消息。
獲取到的數(shù)據(jù)有很多的重復(fù)數(shù)據(jù),既浪費(fèi)流量,也不方便查看信息。
顯示非重復(fù)性的數(shù)據(jù)
對(duì)于顯示重復(fù)性的數(shù)據(jù),這是因?yàn)槲覀儧](méi)有使用where語(yǔ)句,而好似每次都獲取到所有的數(shù)據(jù)了。試想一下,怎樣才能獲取到最新的數(shù)據(jù)呢?
而且對(duì)于不同的客戶(hù)端都要照顧得到。
好萊塢原則:不要來(lái)找我,我會(huì)去找你
這也是很多軟件開(kāi)發(fā)理念的一個(gè)體現(xiàn),讓客戶(hù)決定來(lái)獲取什么數(shù)據(jù),而不是服務(wù)器端一棍子打死。所以我們需要在客戶(hù)端發(fā)送數(shù)據(jù)請(qǐng)求方面最優(yōu)化。
<script>// 記錄當(dāng)前獲取到的id的最大值,防止獲取到重復(fù)的信息var maxId=0;function showmessage(){ var ajax=new XMLHttpRequest(); // 從服務(wù)器獲取并處理數(shù)據(jù) ajax.onreadystatechange=function(){ if(ajax.readyState==4) { //alert(ajax.responseText); // 將獲取到的字符串轉(zhuǎn)換成實(shí)體 eval('var data='+ajax.responseText); // 遍歷data數(shù)組,把內(nèi)部的信息一個(gè)個(gè)的顯示到頁(yè)面上 var s=""; for(var i=0 ; i < data.length;i++){ data[i]; s +="("+data[i].add_time+") >>>"; s +="<p style='color:"+data[i].color+";'>"; s +=data[i].sender +" 對(duì) " + data[i].receiver +" "+ data[i].biaoqing+"說(shuō):" + data[i].msg; s +="</p>"; // 把已經(jīng)獲得的最大的記錄id更新 maxId=data[i].id; } // 開(kāi)始向頁(yè)面時(shí)追加信息 var showmessage=document.getElementById("up"); showmessage.innerHTML +=s; //showmessage.scrollTop 可以實(shí)現(xiàn)div底部最先展示 // divnode.scrollHeight而已獲得div的高度包括滾動(dòng)條的高度 showmessage.scrollTop=showmessage.scrollHeight-showmessage.style.height; } } ajax.open('get','./chatroom.php?maxId='+maxId); ajax.send(null); }// 更新信息的執(zhí)行時(shí)機(jī)window.onload=function(){ //showmessage(); // 制作輪詢(xún),實(shí)現(xiàn)自動(dòng)的頁(yè)面更新 setInterval("showmessage()",3000); }</script>123456789101112131415161718192021222324252627282930313233343536373839404142
優(yōu)化顯示
優(yōu)化顯示界面是必不可少的,沒(méi)有人能夠容忍發(fā)送一條數(shù)據(jù)之后還得手動(dòng)的查看最新的消息。所以我們要設(shè)置一下顯示區(qū)域的div。
加上滾動(dòng)條
<style> #up { height:320px; width:100%; overflow:auto; }</style>1234567
每次都顯示最新消息
說(shuō)白了就是讓底部的div永遠(yuǎn)最先顯示。
//showmessage.scrollTop 可以實(shí)現(xiàn)div底部最先展示// divnode.scrollHeight而已獲得div的高度包括滾動(dòng)條的高度showmessage.scrollTop=showmessage.scrollHeight-showmessage.style.height;123
完整代碼
前端代碼
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Ajax 聊天室</title><style> #up { height:320px; width:100%; overflow:auto; }</style><script>// 記錄當(dāng)前獲取到的id的最大值,防止獲取到重復(fù)的信息var maxId=0;function showmessage(){ var ajax=new XMLHttpRequest(); // 從服務(wù)器獲取并處理數(shù)據(jù) ajax.onreadystatechange=function(){ if(ajax.readyState==4) { //alert(ajax.responseText); // 將獲取到的字符串轉(zhuǎn)換成實(shí)體 eval('var data='+ajax.responseText); // 遍歷data數(shù)組,把內(nèi)部的信息一個(gè)個(gè)的顯示到頁(yè)面上 var s=""; for(var i=0 ; i < data.length;i++){ data[i]; s +="("+data[i].add_time+") >>>"; s +="<p style='color:"+data[i].color+";'>"; s +=data[i].sender +" 對(duì) " + data[i].receiver +" "+ data[i].biaoqing+"說(shuō):" + data[i].msg; s +="</p>"; // 把已經(jīng)獲得的最大的記錄id更新 maxId=data[i].id; } // 開(kāi)始向頁(yè)面時(shí)追加信息 var showmessage=document.getElementById("up"); showmessage.innerHTML +=s; //showmessage.scrollTop 可以實(shí)現(xiàn)div底部最先展示 // divnode.scrollHeight而已獲得div的高度包括滾動(dòng)條的高度 showmessage.scrollTop=showmessage.scrollHeight-showmessage.style.height; } } ajax.open('get','./chatroom.php?maxId='+maxId); ajax.send(null); }// 更新信息的執(zhí)行時(shí)機(jī)window.onload=function(){ //showmessage(); // 制作輪詢(xún),實(shí)現(xiàn)自動(dòng)的頁(yè)面更新 setInterval("showmessage()",3000); }</script></head><body style="background-color:silver"><div id="main"> <h3>消息顯示區(qū)</h3> <div id="up"> </div> <hr /> <h3>發(fā)言欄</h3> <div id="bottom"> <form action="./chatroom_insert.php"> <div id="chat_up"> <span>顏色</span> <input type="color" name="color"/> <span>表情</span> <select name="biaoqing"> <option value="微笑地">微笑地</option> <option value="猥瑣地">猥瑣地</option> <option value="和藹地">和藹地</option> <option value="目不轉(zhuǎn)睛地">目不轉(zhuǎn)睛地</option> <option value="傻傻地">傻傻地</option> </select> <span>聊天對(duì)象</span> <select name="receiver"> <option value="">所有的人</option> <option value="老郭">老郭</option> <option value="小郭">小郭</option> <option value="大郭">大郭</option> </select> </div> <div id="chat_bottom"><script> function send(){ // 向服務(wù)器差入相關(guān)的數(shù)據(jù) var form=document.getElementsByTagName('form')[0]; var formdata=new FormData(form); var xhr=new XMLHttpRequest(); xhr.onreadystatechange=function(){ if(xhr.readyState==4) { //alert(xhr.resposneText); document.getElementById("result").innerHTML=xhr.responseText; setTimeout("hideresult()",2000); } } xhr.open('post','./chatroom_insert.php'); xhr.send(formdata); document.getElementById("msg").value=""; //return false; } // 2秒后實(shí)現(xiàn)提示信息的消失 function hideresult(){ document.getElementById('result').innerHTML=""; }</script> <textarea id="msg" name="msg" style="width:380px;height:auto;"></textarea> <input type="button" value="發(fā)言" onclick="send()" /> 發(fā)言:<span id="result"></span> </div> </form> </div></div></body></html>123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
數(shù)據(jù)庫(kù)表結(jié)構(gòu)
mysql> desc message; +----------+--------------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra | +----------+--------------+------+-----+---------+----------------+| id | int(100) | NO | PRI | NULL | auto_increment | | msg | varchar(255) | NO | | NULL | | | sender | varchar(30) | NO | | NULL | | | receiver | varchar(30) | NO | | NULL | | | color | varchar(10) | YES | | NULL | | | biaoqing | varchar(10) | YES | | NULL | | | add_time | datetime | YES | | NULL | |+----------+--------------+------+-----+---------+----------------+ 7 rows in set (0.00 sec)12345678910111213
服務(wù)器端代碼
<?php// 獲得最新的聊天信息$conn=mysql_connect('localhost','root','mysql'); mysql_select_db('test'); mysql_query('set names utf8');$maxId=$_GET['maxId'];// 防止獲取重復(fù)數(shù)據(jù),本次請(qǐng)求的記錄結(jié)果id要大魚(yú)上次獲得的id$sql="select * from message where id >"."'$maxId'";$qry=mysql_query($sql);$info=array();while($rst=mysql_fetch_assoc($qry)){ $info[]=$rst; }// 通過(guò)json格式給客戶(hù)端提供數(shù)據(jù)echo json_encode($info);?>12345678910111213141516171819202122232425
總結(jié)與展望
總結(jié)
完整的小例子就是這樣了。回顧一下,今天的收獲有:
如何輪詢(xún)獲得數(shù)據(jù),借助了setInterval函數(shù)
定時(shí)消失提示的數(shù)據(jù),借助了setTimeout函數(shù)
如何獲取最新數(shù)據(jù):有客戶(hù)端控制發(fā)送的maxId參數(shù)。
如何優(yōu)化顯示:overflow實(shí)現(xiàn)滾動(dòng)效果;divnode.scrollTop控制顯示底部特效
展望
也許你會(huì)發(fā)現(xiàn),客戶(hù)端發(fā)送人是固定的,那就是因?yàn)樵蹅儧](méi)有做用戶(hù)登錄。如果做了用戶(hù)登錄,我們的發(fā)送人就可以從Session里面動(dòng)態(tài)的獲取。這樣也能更符合人們的主觀(guān)感受。
界面做的比較爛,沒(méi)有加美化效果。加上Bootstrap后效果應(yīng)該會(huì)很棒。
手機(jī)適配效果不好,另外在WindowsPhone上面顏色控件不能正常的顯示。
意事項(xiàng)
1、像MySQL配置文件、Nginx配置文件、網(wǎng)站根目錄這種比較經(jīng)常操作的需要先使用 docker cp 將文件從容器里復(fù)制到主機(jī)目錄,docker run的時(shí)候直接掛載目錄就可以了
2、docker run MySQL和Nginx的時(shí)候注意使用 --link實(shí)現(xiàn)容器之間通信
3、運(yùn)行容器的時(shí)候注意使用 --ip 固定容器IP
/var/lib/mysql
/etc/mysql/conf.d
/var/run/mysqld
# 主機(jī)目錄:容器目錄
/docker-data/web/mysql/data:/var/lib/mysql
/docker-data/web/mysql/conf:/etc/mysql/conf.d
/docker-data/web/mysql/mysql_sock:/var/run/mysqld
將mysql的配置文件,自帶數(shù)據(jù)庫(kù),sock配置 復(fù)制到主機(jī)目錄
# docker cp container_ID:容器目錄 主機(jī)目錄
docker cp 2d19ae0acd5d:/var/lib/mysql /docker-data/web/mysql/data
docker cp 2d19ae0acd5d:/etc/mysql/conf.d /docker-data/web/mysql/conf
docker cp 2d19ae0acd5d:/var/run/mysqld /docker-data/web/mysql/mysql_sock
docker run
--name mysql # 指定容器名稱(chēng)
-e MYSQL_ROOT_PASSWORD=root # 初始化root用戶(hù)的密碼為root
-p 3307:3306 # 將容器的3306端口映射到主機(jī)的3307端口
--privileged=true # 給容器加上特定的權(quán)限,否則可能會(huì)出現(xiàn)常見(jiàn)容器失敗的情況
-v /docker-data/web/mysql/data:/var/lib/mysql # 掛載目錄 mysql數(shù)據(jù)文件
-v /docker-data/web/mysql/conf:/etc/mysql/conf.d # 掛載目錄 mysql配置文件
-v /docker-data/web/mysql/mysql_sock:/var/run/mysqld # 掛載目錄 mysql mysqld
--restart=always # 設(shè)置失敗自動(dòng)重啟 可寫(xiě)為 --restart=on-failure:5 最多重啟五次
-d mysql:5.7
docker run --name mysql -e MYSQL_ROOT_PASSWORD=root -p 3307:3306 --privileged=true -v /docker-data/web/mysql/data:/var/lib/mysql -v /docker-data/web/mysql/conf:/etc/mysql/conf.d -v /docker-data/web/mysql/mysql_sock:/var/run/mysqld --restart=always-d mysql:5.7
/var/www/html
/var/run/mysqld
# 主機(jī)目錄:容器目錄
/docker-data/web/www/:/var/www/html
/docker-data/web/mysql/mysql_sock/:/var/run/mysqld
# docker cp container_ID:容器目錄 主機(jī)目錄
docker cp 7da668050b8a:/var/www/html /docker-data/web/www
docker run
--name php
-p 9000:9000
--privileged=true
-v /docker-data/web/www/:/wwwroot
-v /docker-data/web/mysql/mysql_sock/:/var/run/mysqld
--link mysql:mysql # 使用link實(shí)現(xiàn)兩個(gè)容器之間的通信
--restart=always
-d
php:7.2-fpm
docker run --name php -p 9000:9000 --privileged=true -v /docker-data/web/www/:/wwwroot -v /docker-data/web/mysql/mysql_sock/:/var/run/mysqld --link mysql:mysql --restart=always -d php:7.2-fpm
# 進(jìn)入容器執(zhí)行命令安裝擴(kuò)展
docker-php-ext-install [擴(kuò)展名稱(chēng)]
# 安裝GD庫(kù)
apt update #更新軟件源
apt install -y libwebp-dev libjpeg-dev libpng-dev libfreetype6-dev #安裝各種庫(kù)
docker-php-source extract #解壓源碼
cd /usr/src/php/ext/gd #gd源碼文件夾
docker-php-ext-configure gd --with-webp-dir=/usr/include/webp --with-jpeg-dir=/usr/include --with-png-dir=/usr/include --with-freetype-dir=/usr/include/freetype2 #準(zhǔn)備編譯
docker-php-ext-install gd #編譯安裝
php -m | grep gd
docker restart [container ID]#重啟容器
/usr/share/nginx/html
/etc/nginx/conf.d
# 主機(jī)目錄:容器目錄
/docker-data/web/www:/usr/share/nginx/html
/docker-data/web/nginx/conf.d:/etc/nginx/conf.d
# docker cp container_ID:容器目錄 主機(jī)目錄
docker cp e0babc8a32ad:/usr/share/nginx/html /docker-data/web/www
docker cp e0babc8a32ad:/etc/nginx/conf.d /docker-data/web/nginx/conf.d
docker run
--name nginx
-p 8080:80
--privileged=true
-v /docker-data/web/www:/wwwroot
-v /docker-data/web/nginx/conf.d:/etc/nginx/conf.d
--link php:php
--restart=always
-d
nginx
docker run --name nginx -p 8080:80 --privileged=true -v /docker-data/web/www:/wwwroot -v /docker-data/web/nginx/conf.d:/etc/nginx/conf.d --link php:php --restart=always -d nginx
server {
listen 80;
server_name tplay.yulongcode.com;
root /wwwroot/tplay/public; # 項(xiàng)目在容器里的目錄
index index.php index.html index.htm;
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /index.php?s=$1 last; break;
}
}
error_page 500 502 503 504 /50x.html;
location ~ \.php$ {
fastcgi_pass PHP容器IP:PHP端口;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
let the world have no hard-to-write code
作者:汪玉龍
原文:https://www.cnblogs.com/yulongcode/p/12719548.html
MySQL 是最流行的關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng)(非關(guān)系型數(shù)據(jù)庫(kù)簡(jiǎn)略介紹) 關(guān)系數(shù)據(jù)庫(kù)管理系統(tǒng)(Relational Database Management System)的特點(diǎn) 數(shù)據(jù)以表格的形式出現(xiàn) 每行為各種記錄名稱(chēng) 許多的行和列組成一張表單 若干的表單組成database 主鍵:主鍵是唯一的。一個(gè)數(shù)據(jù)表中只能包含一個(gè)主鍵。你可以使用主鍵來(lái)查詢(xún)數(shù)據(jù)。
數(shù)值類(lèi)型
日期和時(shí)間類(lèi)型
字符串類(lèi)型
<?php
# 下面就是建立鏈接,$link 會(huì)得到一個(gè)鏈接信息
$link=mysql_connect('ip地址', '數(shù)據(jù)庫(kù)用戶(hù)名', '數(shù)據(jù)庫(kù)密碼');
?>
<?php
# 下面就是確定你要操作哪個(gè)庫(kù)
mysql_select_db('你要操作的庫(kù)的名稱(chēng)', $link);
?>
<?php
# 下面就是使用 sql 語(yǔ)句對(duì)數(shù)據(jù)庫(kù)進(jìn)行操作
$res=mysql_query('你要執(zhí)行的 sql 語(yǔ)句');
?>
<?php
mysql_close($conn);
?>
<?php
$conn=mysql_connect('localhost', 'root', 'root');
mysql_select_db('test1913');
$res=mysql_query('SELECT * FROM `student`');
$row=mysql_fetch_assoc($res);
mysql_close($conn);
?
print_r($row);
?>
<?php
# 查詢(xún) student 這個(gè)表里面的所有數(shù)據(jù)
$sql='SELECT * FROM `student`';
# 查詢(xún) student 表中的數(shù)據(jù)里面 gender 為 男 的數(shù)據(jù)
$sql='SELECT * FROM `student` WHERE `gender`="男"';
# 查詢(xún) student 表中的數(shù)據(jù)里面 age 大于 18 的數(shù)據(jù)
$sql='SELECT * FROM `student` WHERE `age`>18';
# 查詢(xún) student 表中的數(shù)據(jù)里面 age 大于 18 且 gender 為 男 的數(shù)據(jù)
$sql='SELECT * FROM `student` WHERE `age`>18 AND `gender`="男"';
?
# 查詢(xún) student 表中的數(shù)據(jù)里面 age 小于 22 或者 age 大于 28 的數(shù)據(jù)
$sql='SELECT * FROM `student` WHERE `age`<22 OR `age`>28';
?
# 查詢(xún) student 表中的數(shù)據(jù)里面從 第幾條開(kāi)始 查詢(xún)多少條
$sql='SELECT * FROM `student` LIMIT 0, 10';
# 先按照條件篩選出數(shù)據(jù)以后再進(jìn)行分頁(yè)查詢(xún)
# 下面是查詢(xún)表中所有 age>18 且 性別為男的所有數(shù)據(jù),查出來(lái)以后從第 10 條開(kāi)始查 10 條
$sql='SELECT * FROM `student` WHERE `age`>18 AND `gender`="男" LIMIT 10, 10';
?
# 查詢(xún)表的模糊查詢(xún)
# 下面表示查詢(xún)表中所有數(shù)據(jù)里面 name 字段中包含 "三" 字的數(shù)據(jù)
$sql='SELECT * FROM `student` WHERE `name` LIKE "%三%"';
?
# 查詢(xún)排序,查詢(xún)的時(shí)候按照某一個(gè)字段升序或降序排序
$sql='SELECT * FROM `student` ORDER BY `age` ASC';
$sql='SELECT * FROM `student` ORDER BY `age` DESC';
?>
<?php
# 向表中增加一條數(shù)據(jù),再增加的時(shí)候主鍵不能由我們書(shū)寫(xiě),而是 mysql 數(shù)據(jù)庫(kù)自己遞增
$sql='INSERT INTO `student` VALUES(null, "張三", 18, "男", 1913, 100)';
# 插入固定幾個(gè)鍵的數(shù)據(jù),其他的用默認(rèn)值
$sql='INSERT INTO `student` (`name`, `age`) VALUES("李四", 22)';
?>
<?php
# 刪除表中 id 為 100 的數(shù)據(jù)
$sql='DELETE FROM `student` WHERE `id`=100';
?
# 刪除表中 name 為 張三 的數(shù)據(jù)
$sql='DELETE FROM `student` WHERE `name`="張三"'
?>
*請(qǐng)認(rèn)真填寫(xiě)需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。