PHP 自学教程之MySQL数据库
生活随笔
收集整理的這篇文章主要介紹了
PHP 自学教程之MySQL数据库
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
PHP訪問MySQL數(shù)據(jù)庫的一般步驟: 1、連接MySQL數(shù)據(jù)庫:使用mysql_connect()函數(shù)建立與MySQL服務(wù)器的連接。 2、選擇MySQL數(shù)據(jù)庫:使用mysql_select_db()函數(shù)選擇MySQL數(shù)據(jù)庫服務(wù)器上對(duì)于的數(shù)據(jù)庫。 3、執(zhí)行SQL語句:在選擇的數(shù)據(jù)庫中使用mysql_query()函數(shù)執(zhí)行SQL語句。 4、關(guān)閉結(jié)果集:數(shù)據(jù)庫操作完畢后,通過mysql_free_result()函數(shù),釋放MySQL系統(tǒng)資源。 5、關(guān)閉MySQL服務(wù)器:在完成數(shù)據(jù)庫操作,應(yīng)該使用mysql_close()函數(shù)關(guān)閉與數(shù)據(jù)庫服務(wù)器連接。 PHP連接MySQL數(shù)據(jù)庫實(shí)例代碼:
結(jié)果展示:
最新內(nèi)容請(qǐng)見作者的GitHub頁:http://qaseven.github.io/
| <?php //定義連接數(shù)據(jù)庫相關(guān)變量 $dbhost ="localhost" ; $dbuser = "swxm"; // 我的用戶名 $dbpass = "swxm"; // 我的密碼 $dbname = "shopping"; // 我的mysql庫名 //連接到數(shù)據(jù)庫函數(shù)---mysql_connect $connection=mysql_connect($dbhost,$dbuser,$dbpass); if(!$connection){ die("無法連接到MySQL數(shù)據(jù)庫:</br>".mysql_error());//診斷連接錯(cuò)誤 } //選擇數(shù)據(jù)庫函數(shù)--------mysql_select_db $db_selecct=mysql_select_db($dbname, $connection); if(!$db_selecct) { die("無法連接到指定的數(shù)據(jù)庫</br>".mysql_error()); } $query="select * from ?user ";//構(gòu)建查詢語句 //執(zhí)行SQL查詢函數(shù)-----------mysql_query $result=mysql_query($query); if(!$result) { die("無法進(jìn)行相關(guān)查詢操作</br>".mysql_error()); } //查詢結(jié)果 while($result_row=mysql_fetch_row($result))//取出結(jié)果并顯示 { $num=$result_row[0]; $age=$result_row[1]; $name=$result_row[2]; echo "<tr>"; echo "<td>$num</td><br/>"; echo "<td>$age</td><br/>"; echo "<td>$name</td>"; echo "</tr>"; } ?> |
最新內(nèi)容請(qǐng)見作者的GitHub頁:http://qaseven.github.io/
總結(jié)
以上是生活随笔為你收集整理的PHP 自学教程之MySQL数据库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 亚马逊智能语音助手Alexa大举招聘 放
- 下一篇: linux cmake编译源码,linu