您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)使用php怎么從數(shù)據(jù)庫(kù)中獲取數(shù)據(jù)并利用ajax傳送到前臺(tái),小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
1、數(shù)據(jù)庫(kù)的字段:
2、php連接數(shù)據(jù)庫(kù)獲取數(shù)據(jù)庫(kù)的信息放入json_encode($css);{文件為:db.php}
<span ><?php $host="localhost"; $username="root"; $password="root"; $dbName="baixing"; $port=3306; $conn=new mysqli($host,$username,$password,$dbName,$port); if(!$conn){ die("error:".$conn->connect_error); } //設(shè)置查詢結(jié)果的編碼,一定要放在query之前 $conn->query("SET NAMES 'UTF8'"); $result=$conn->query("select * from hotgoods"); //$conn->query()獲取的是二進(jìn)制 //將查詢的結(jié)果集封裝到一個(gè)數(shù)組里 $css=$result->fetch_all(); //以json的格式發(fā)送ajax的success中由data接收 echo json_encode($css); $conn->close();</span>
3、將json_encode($css)的數(shù)據(jù)放在success:function(data)中,如下:{baixing.html}
$.ajax({ type: 'POST', url: 'db.php', data:{ // "username":"admin", // "password":"123456" }, success: function (data) { var result=eval("("+data+")"); alert(result); for(var i=0;i<result.length;i++){ var str='<div class="home1">'+ '<img src="'+result[i][1]+'" alt="'+result[i][3]+'"/>'+ '<p><a href="'+result[i][2]+'" rel="external nofollow" rel="external nofollow" >'+result[i][3]+'</a></p>'+ '<div class="price">'+ '<span>¥'+result[i][4]+'</span>'+ '<del>¥'+result[i][5]+'</del>'+ ' <a href="#" rel="external nofollow" rel="external nofollow" >預(yù)定:<b>'+result[i][6]+'</b>件</a>' '</div> </div>' $(".box7 #hotSale").append(str);//追加到你需要放在的位置 } } });$.ajax({ type: 'POST', url: 'db.php', data:{ // "username":"admin", // "password":"123456" }, success: function (data) { var result=eval("("+data+")"); alert(result); for(var i=0;i<result.length;i++){ var str='<div class="home1">'+ '<img src="'+result[i][1]+'" alt="'+result[i][3]+'"/>'+ '<p><a href="'+result[i][2]+'" rel="external nofollow" rel="external nofollow" >'+result[i][3]+'</a></p>'+ '<div class="price">'+ '<span>¥'+result[i][4]+'</span>'+ '<del>¥'+result[i][5]+'</del>'+ ' <a href="#" rel="external nofollow" rel="external nofollow" >預(yù)定:<b>'+result[i][6]+'</b>件</a>' '</div> </div>' $(".box7 #hotSale").append(str); } } });
php是一個(gè)嵌套的縮寫名稱,是英文超級(jí)文本預(yù)處理語(yǔ)言,它的語(yǔ)法混合了C、Java、Perl以及php自創(chuàng)新的語(yǔ)法,主要用來做網(wǎng)站開發(fā),許多小型網(wǎng)站都用php開發(fā),因?yàn)閜hp是開源的,從而使得php經(jīng)久不衰。
以上就是使用php怎么從數(shù)據(jù)庫(kù)中獲取數(shù)據(jù)并利用ajax傳送到前臺(tái),小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。