php連接mysql資料庫
PHP訪問MYSQL資料庫的五個步驟
1.連接MySQL資料庫
使用 mysqli_connect()函式建立與MySQL服務器的連接,
2.選擇MySQL資料庫
使用 mysqli_select_db()函式選擇MySQL資料庫服務器的資料庫,
3.執行 SQL 陳述句
在選擇資料庫中使用 mysqli_query()函式執行 SQL陳述句,對資料的操作方式主要包括 5種方式,下面我們分別進行介紹,
-
查詢資料:使用select 陳述句實作資料的查詢功能,
-
顯示資料:使用select 陳述句顯示資料的查詢結果,
-
插入資料:使用insert into 陳述句向資料庫中插入資料,
-
更新資料:使用update 陳述句更新資料庫中的記錄,
-
洗掉資料:使用 delete陳述句洗掉資料庫中的記錄!
4.關閉結果集
資料庫操作完成后,需要關閉結果集,以釋放系統資源,使用mysqli_free_result($result);
5.關閉MySQL服務器
每使用一次 mysqli_connect()或者mysqli_query()函式,都會消耗系統資源,如果用戶連接超過一定數量時,就會造成系統性能下降,甚至是死機,為了避免這種現象的發生,在完成資料庫的操作后,應該使用 mysqli_close()函式關閉與MYSQL服務器的連接,以節省系統資源,
技巧:
如果在多個網頁中都要頻繁進行資料庫訪問,那么可以建立與資料庫服務器的持續連接來提高效率,因為每次與資料庫服務器的連接需要較長的時間和交大的資源開銷,持續的連接相對來說會更有效率,建立持續連接的方法就是在資料庫間接時,呼叫函式 mysqli_pconnect()代替mysqli_connect函式,建立的持續連接在本程式結束時,不需要呼叫 mysqli_colse()來關閉與資料庫服務器的連接,下次程式在此執行 mysqli_pconnect()函式時,系統自動直接回傳已經建立的持續連接ID號,而不再去真的連接資料庫,
PHP 中與資料庫的連接是非持久連接,系統會自動回收,一般不用設定關閉,但是如果一次性訪問的結果集比較大,或者網站訪問量比較多,那么最好使用 mysqli_close()函式手動進行釋放,
使用mysqli_connect()函式連接資料庫
//創建連接 $conn = new mysqli('localhost','root','123456','test'); //檢測連接 if($conn->connect_error){ die('連接失敗:'.$conn->connect_error); }
使用mysqli_select_db()函式選擇資料庫檔案
下面的實體使用了 mysql_select_db()函式連接資料庫,具體實體代碼如下:
$link = mysqli_connect('localhost','root','123456') or die('不能連接到資料庫').mysqli_error(); $conn = mysqli_select_db($link,'test'); if($conn){ echo '資料庫連接成功'; }

上面的代碼$conn = mysqli_select_db($link,'test');可以使用下面的代碼替代:
$conn = mysqli_query($link,'use test');
使用mysqli_query()函式執行SQL陳述句
下面舉例說明常見的 SQL 陳述句的用法,
<?php $conn = mysqli_connect('localhost','root','123456','test') or die('不能連接到資料庫').mysqli_error(); //添加會員記錄 $sql = "INSERT INTO login(username,password,confirm,email) VALUES ('cyy2','123456','123456','[email protected]')"; if(mysqli_query($conn,$sql)){ echo '插入成功'; }else{ echo 'error:'.$sql.'<br>'.mysqli_error($conn); } //修改會員記錄 $sql = "UPDATE login SET username = 'cyy' where id = '3'"; if(mysqli_query($conn,$sql)){ echo '更新成功'; }else{ echo 'error:'.$sql.'<br>'.mysqli_error($conn); } //洗掉會員記錄 $sql = "DELETE FROM login WHERE id = '4' "; if(mysqli_query($conn,$sql)){ echo '洗掉成功'; }else{ echo 'error:'.$sql.'<br>'.mysqli_error($conn); } //查詢會員記錄 $sql = "SELECT * FROM login"; if(mysqli_query($conn,$sql)){ echo '查詢成功'; }else{ echo 'error:'.$sql.'<br>'.mysqli_error($conn); } //顯示表結構 $sql = "DESC login"; if(mysqli_query($conn,$sql)){ echo '顯示成功'; }else{ echo 'error:'.$sql.'<br>'.mysqli_error($conn); }
使用mysqli_fetch_array()獲取陣列結果集中的資訊
mysqli_fetch_array() 函式從結果集中取得一行作為關聯陣列,或數字陣列,或二者兼有回傳根據從結果集取得的行生成的陣列,如果沒有更多行則回傳 false,
注意:
mysqli_fetch_array() 函式回傳的欄位名區分大小寫,
具體開發步驟如下:
1.創建一個PHP動態頁面,命名index.php,在index.php中添加一個表單,一個文本框以及一個提交按鈕,具體代碼如下:
<html>
<body>
<!--上傳檔案表單-->
<form method="post" action="" name = form1>
<table>
<tr>
<td width="605" height="51" bgcolor="#CC99FF">
<p align="center">請輸入查詢內容
<input type="text" name="username" id="username" size="25">
<input type="submit" name="Submit" value="https://www.cnblogs.com/chenyingying0/p/查詢">
</p>
</td>
</tr>
</table>
</form>
</body>
</html>
頁面如下:

2.連接到MySQL資料庫服務器,具體代碼如下:
<?php header("Content-Type:text/html; charset=utf-8"); //連接資料庫 $link = mysqli_connect("localhost","root","123456","test")or die("連接資料庫失敗".mysqli_error()); mysqli_query($link,"set names utf-8"); //設定編碼,防止發生亂碼 ?>
3.使用 if 條件陳述句判斷用戶是否單擊“查詢”按鈕,如果是那么使用POST 方法接受傳遞過來的資訊,執行SQL陳述句,該查詢陳述句主要用來實作資訊的模糊查詢,查詢結果被賦予變數$sql,然后從陣列結果集中獲取資訊,具體代碼如下:
//模糊查詢 $sql = mysqli_query($link,"SELECT * FROM login"); $info = mysqli_fetch_array($sql); if($_POST['submit'] == '查詢'){ $username = $_POST['username']; $sql = mysqli_query($link,"SELECT * FROM login WHERE username LIKE '%".trim($username)."%' "); $info = mysqli_fetch_array($sql); }
4.使用 if條件陳述句對結果集變數$info進行判斷,如果該值為假,那么就使用 echo 陳述句輸出檢索的資訊不存在,具體代碼如下:
//判斷結果 if(!$info){ echo "<p align='center' style='color: #FF0000;font-size: 12px'>對不起,查詢的資訊不存在</p>"; }
5.使用 do...while 回圈陳述句以表格形式輸出陣列結果集$info[]中的資訊,一欄位的名稱為索引,使用 echo 陳述句輸出陣列$info[]的資訊,具體代碼如下:
//回圈輸出結果 do { //do...while 回圈 ?> <table> <tr align="left" bgcolor="#FFFFFF"> <td height="20" align="center"><?php echo $info["id"] ?></td> <td height="20" align="center"><?php echo $info["username"] ?></td> <td height="20" align="center"><?php echo $info["password"] ?></td> <td height="20" align="center"><?php echo $info["confirm"] ?></td> <td height="20" align="center"><?php echo $info["email"] ?></td> </tr> </table> <?php }while($info = mysqli_fetch_array($sql));
輸出結果如下:

使用mysqli_fetch_row()函式逐行獲取結果集中的每條記錄
<html>
<body>
<!--上傳檔案表單-->
<form method="post" action="" name = form1>
<table>
<tr>
<td width="605" height="51" bgcolor="#CC99FF">
<p align="center">請輸入查詢內容
<input type="text" name="username" id="username" size="25">
<input type="submit" name="submit" value="https://www.cnblogs.com/chenyingying0/p/查詢">
</p>
</td>
</tr>
</table>
</form>
<?php
header("Content-Type:text/html; charset=utf-8");
error_reporting(0);
//連接資料庫
$link = mysqli_connect("localhost","root","123456","test")or die("連接資料庫失敗".mysqli_error());
mysqli_query($link,"set names utf-8"); //設定編碼,防止發生亂碼
//模糊查詢
$sql = mysqli_query($link,"SELECT * FROM login");
$info = mysqli_fetch_row($sql);
if($_POST['submit'] == '查詢'){
$username = $_POST['username'];
$sql = mysqli_query($link,"SELECT * FROM login WHERE username LIKE '%".trim($username)."%' ");
$info = mysqli_fetch_row($sql);
}
//判斷結果
if(!$info){
echo "<p align='center' style='color: #FF0000;font-size: 12px'>對不起,查詢的資訊不存在</p>";
}
//回圈輸出結果
do { //do...while 回圈
?>
<table>
<tr align="left" bgcolor="#FFFFFF">
<td height="20" align="center"><?php echo $info[0] ?></td>
<td height="20" align="center"><?php echo $info[1] ?></td>
<td height="20" align="center"><?php echo $info[2] ?></td>
<td height="20" align="center"><?php echo $info[3] ?></td>
<td height="20" align="center"><?php echo $info[4] ?></td>
</tr>
</table>
<?php
}while($info = mysqli_fetch_row($sql));
?>
</body>
</html>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/33642.html
標籤:PHP
上一篇:php面向物件高級應用二
下一篇:PHP PDO mysql抽象層
