PHP訪問MySQL的資料--讀書筆記4

來源:互聯網
上載者:User

標籤: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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.