標籤:name 活動 資料庫操作 操作 date har 姓名 lan text
<?php
header(‘Content-Type:text/html;charset=utf-8‘);
// mysqli_connect(‘IP:3306‘,‘帳號‘,‘密碼‘,‘要操作的資料庫名稱‘);
$con=mysqli_connect(‘127.0.0.1‘,‘root‘,‘123.‘school‘);
//設定編碼
if($conn){
echo ‘連結成功‘;
}
mysqli_set_charset($con,‘utf8‘);
//資料庫操作【例如所有學生的名單】$sqsl=sql語句
$sql="SELECT `id`,`name`,`sex`,`class`,`age`,`description` FROM `student`";
$res = mysqli_query($con,$sql);
/*
第一:mysql_select_db() 函數設定活動的 MySQL 資料庫。
如果成功,則該函數返回 true。如果失敗,則返回 false。
第二
mysqli_query的使用有兩種情況:
1,如果第二個參數的sql語句是查詢語句 select則,返回的結果就是一個集合/false
2,如果第二個參數的sql語句是其他動作update、insert、delete。則返回的結果就是true、false
3,mysqli_query(connection,query,resultmode);
| 參數 |
描述 |
| connection |
必需。規定要使用的 MySQL 串連。 |
| query |
必需,規定查詢字串。 |
| resultmode |
可選。一個常量。可以是下列值中的任意一個:
- MYSQLI_USE_RESULT(如果需要檢索大量資料,請使用這個)
- MYSQLI_STORE_RESULT(預設)
|
*/
//從結果集中提取資料
//開發時,使用最多的是前面兩種。
//$item = mysqli_fetch_assoc($res); //以關聯陣列的形式來提取結果集中的一行資料
//$item = mysqli_fetch_object($res); //以對象的形式來提取結果集中的一行資料
//$item = mysqli_fetch_row($res); //以索引數組的形式來提取結果集中的一行資料
//$item = mysqli_fetch_array($res); //以索引+關聯陣列的形式來提取結果集中的一行資料
// mysqli_fetch_assoc 如果擷取不到資料了,則返回 false
$data = []; //聲明一個空數組,用來儲存資料
while( $item = mysqli_fetch_assoc($res) ){
$data[] = $item;
}
mysqli_close($conn);
//print_r( $data );
?>
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<table border="1" width="800" align="center">
<tr>
<td>學號ID</td>
<td>姓名</td>
<td>性別</td>
<td>年齡</td>
<td>班級</td>
<td>個性簽名</td>
</tr>
<?php foreach($data as $item): ?>
<tr>
<td><?=$item[‘id‘]; ?></td>
<td><?=$item[‘name‘]; ?></td>
<td><?=$item[‘sex‘]; ?></td>
<td><?=$item[‘age‘]; ?></td>
<td><?=$item[‘class‘]; ?></td>
<td><?=$item[‘description‘]; ?></td>
</tr>
<?php endforeach; ?>
</table>
</body>
</html>
PHP訪問MySQL的資料--讀書筆記4