PHP運算元據庫 展示員工資訊

2021-10-10 15:22:52 字數 2209 閱讀 4173

1、建立員工資訊表、插入資料。

create table `emp_info` (

`e_id` int unsigned primary key auto_increment,

`e_name` varchar(20) not null comment '姓名',

`e_gender` enum('男','女') default '男' not null comment '性別',

`e_dept` varchar(20) not null comment '所屬部門',

`date_of_birthday` date not null comment '出生日期',

`date_of_entry` timestamp not null comment '入職日期'

)charset=utf8;

insert into `emp_info` (`e_id`,`e_name`,`e_gender`,`e_dept`,`date_of_birthday`,`date_of_entry`) values

(1,'張三','市場部','男','2020-11-19','2014-9-21 17:00:00'),

(2,'李四','開發部','男','2020-11-19','2013-9-21 17:00:00'),

(3,'王五','**部','女','2020-11-19','2015-9-21 17:00:00'),

(4,'趙六','銷售部','女','2020-11-19','2015-9-20 17:00:00');

2、建立show.php檔案,獲取員工資料。

<?php

header('content-type:text/html; charset=utf-8');//宣告http訊息頭的文件編碼格式

$link=mysql_connect('localhost','root','root');//連線資料庫

if(!$link)

mysql_query('set names utf8');//設定字符集

mysql_query('use `users`');//選擇資料庫

$sql="select * from `emp_info`";//準備sql語句

$res=mysql_query($sql,$link);//執行sql語句,獲取結果集。返回值是資源型別資料

$emp_info=array();//定義員工陣列

while($row=mysql_fetch_assoc($res))//mysql_fetch_assoc($res)處理結果集,while()遍歷處理後的結果集

require './show_html.php';

3、建立show_html.php,列表展示。

id姓名

性別所屬部門

出生日期

入職日期

操作 <?php

if(!empty($emp_info))

}else

?>

(1)宣告http訊息頭的文件編碼格式:

header('content-type:text/html; charset=utf-8');
(2)連線、選擇資料庫:

$link=mysql_connect('要連線的資料庫位址','運算元據庫的使用者名稱','運算元據庫使用者的密碼');

if(!$link)

(3)設定字符集,選擇資料庫:

mysql_query('set names utf8');

mysql_query('use `資料庫名`');

(4)執行sql語句,獲取結果集:

$res=mysql_query($sql,$link);//返回值是資源型別而非陣列
(5)處理結果集:

從結果集中讀取一條資料,以下列形式返回:

返回值是索引陣列:mysql_fetch_row();

返回值是關聯陣列:mysql_fetch_assoc();

同時返回索引陣列和關聯陣列:mysql_fetch_array();

返回值是物件:mysql_fetch_object();

(6)釋放資源

清除結果集:mysql_free_result();

關閉資料庫連線:mysql_close();

php 運算元據庫

本例是用php連線乙個mysql資料庫操作的演示,實現連線開啟乙個庫,並讀取資料的基本功能。資料庫名稱為 dbname 表名為 person 分別有7個字段 id userid age tel email address 伺服器 資料庫編碼 均採用 utf 8 mysql query set nam...

PHP運算元據庫

很簡單,只需要幾行 就搞定!1.連線資料庫 pdo new pdo mysql host localhost dbname 資料庫名 使用者名稱 密碼 2.準備sql語句 result pdo prepare select from user where id 3.執行哦sql語句 result e...

PHP運算元據庫

1.1.1.連線資料庫基本步驟 1.連線資料庫 2.準備sql語句 3.執行sql語句 4.獲取執行的結果並分析 5.關閉資料庫 1.1.2.運算元據庫常用api mysqli connect ip,使用者名稱,密碼,資料庫名 連線資料庫 mysqli query link,sql 執行sql語句 ...