# yum -y install httpd
# rpm -qa | grep httpd
httpd-tools-2.4.6-89.el7.centos.1.x86_64
httpd-2.4.6-89.el7.centos.1.x86_64
# systemctl start httpd.service
# systemctl enable httpd.service
# systemctl status httpd.service
# firewall-cmd --add-service=http --permanent
# firewall-cmd --reload
# firewall-cmd --list-all
http://192.168.1.9/
# yum -y install php
# rpm -qa | grep php
php-common-5.4.16-46.el7.x86_64
php-5.4.16-46.el7.x86_64
php-cli-5.4.16-46.el7.x86_64
# systemctl restart httpd.service
# vi /var/www/html/test.php
<?php
phpinfo();
php?>
http://192.168.1.9/test.php
-------------- MariaDB Ctrl-C -- exit ------
# yum -y install mariadb-server
# rpm -qa | grep mariadb
mariadb-5.5.60-1.el7_5.x86_64
mariadb-server-5.5.60-1.el7_5.x86_64
mariadb-libs-5.5.60-1.el7_5.x86_64
# systemctl start mariadb.service
# systemctl is-active mariadb.service
# systemctl enable mariadb.service
# systemctl status mariadb.service
# mysqladmin -u root password db123456
# mysql -u root -p //登入 MariaDB
Enter password:
MariaDB [(none)]>
MariaDB [(none)]> select @@datadir; //資料庫儲存路徑
+-----------------+
| @@datadir |
+-----------------+
| /var/lib/mysql/ |
+-----------------+
1 row in set (0.00 sec)
--------------- 伺服器字元集 character set 和連線校對 collaction -------------
MariaDB [(none)]> show variables like 'character_set_server';
+----------------------+--------+
| Variable_name | Value |
+----------------------+--------+
| character_set_server | latin1 |
+----------------------+--------+
--------------- 資料庫字元集 character set 和連線校對 collaction -------------
MariaDB [(none)]> show variables like 'character_set_database';
+------------------------+--------+
| Variable_name | Value |
+------------------------+--------+
| character_set_database | latin1 |
+------------------------+--------+
MariaDB [(none)]> show variables like 'collation_database';
+--------------------+-----------------+
| Variable_name | Value |
+--------------------+-----------------+
| collation_database | utf8_general_ci |
+--------------------+-----------------+
--------------- 資料表字元集 character set 和連線校對 collaction -------------
MariaDB [class]> create table tblmen (mid int primary key auto_increment, na varchar(20), gra varchar(1), cla varchar(2), snum varchar(2), pwd varchar(10)) character set 'utf8' collate 'utf8_unicode_ci';
MariaDB [class]> show create table tblmen \G; *************************** 1. row ***************************
Table: tblmen
Create Table: CREATE TABLE `tblmen` (
`mid` int(11) NOT NULL AUTO_INCREMENT,
`na` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
`gra` varchar(1) COLLATE utf8_unicode_ci DEFAULT NULL,
`cla` varchar(2) COLLATE utf8_unicode_ci DEFAULT NULL,
`snum` varchar(2) COLLATE utf8_unicode_ci DEFAULT NULL,
`pwd` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`mid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
MariaDB [(none)]> select @@datadir; //資料庫儲存路徑
+-----------------+
| @@datadir |
+-----------------+
| /var/lib/mysql/ |
+-----------------+
1 row in set (0.00 sec)
--------------- 伺服器字元集 character set 和連線校對 collaction -------------
MariaDB [(none)]> show variables like 'character_set_server';
+----------------------+--------+
| Variable_name | Value |
+----------------------+--------+
| character_set_server | latin1 |
+----------------------+--------+
MariaDB [(none)]> set character_set_server='utf8';
MariaDB [(none)]> show variables like 'character_set_server';
+----------------------+-------+
| Variable_name | Value |
+----------------------+-------+
| character_set_server | utf8 |
+----------------------+-------+
MariaDB [(none)]> show variables like 'collation_server';
+------------------+-------------------+
| Variable_name | Value |
+------------------+-------------------+
| collation_server | latin1_swedish_ci |
+------------------+-------------------+
MariaDB [(none)]> set collation_server='utf8_unicode_ci' ;
MariaDB [(none)]> show variables like 'collation_server';
+------------------+-----------------+
| Variable_name | Value |
+------------------+-----------------+
| collation_server | utf8_unicode_ci |
+------------------+-----------------+
--------------- 資料庫字元集 character set 和連線校對 collaction -------------
MariaDB [(none)]> show variables like 'character_set_database';
+------------------------+--------+
| Variable_name | Value |
+------------------------+--------+
| character_set_database | latin1 |
+------------------------+--------+
MariaDB [(none)]> set character_set_database='utf8';
MariaDB [(none)]> show variables like 'character_set_database';
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| character_set_database | utf8 |
+------------------------+-------+
MariaDB [(none)]> show variables like 'collation_database';
+--------------------+-----------------+
| Variable_name | Value |
+--------------------+-----------------+
| collation_database | utf8_general_ci |
+--------------------+-----------------+
------方法2 建立料庫時在設定-----------
MariaDB [kkdb]> create database testdb character set='utf8' collate='utf8_unicode_ci';
MariaDB [testdb]> show variables like '%database';
+------------------------+-----------------+
| Variable_name | Value |
+------------------------+-----------------+
| character_set_database | utf8 |
| collation_database | utf8_unicode_ci |
| skip_show_database | OFF |
+------------------------+-----------------+
--------------- 資料表字元集 character set 和連線校對 collaction -------------
MariaDB [class]> create table tblmen (mid int primary key auto_increment, na varchar(20), gra varchar(1), cla varchar(2), snum varchar(2), pwd varchar(10)) character set 'utf8' collate 'utf8_unicode_ci';
Table: tblmen
Create Table: CREATE TABLE `tblmen` (
`mid` int(11) NOT NULL AUTO_INCREMENT,
`na` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
`gra` varchar(1) COLLATE utf8_unicode_ci DEFAULT NULL,
`cla` varchar(2) COLLATE utf8_unicode_ci DEFAULT NULL,
`snum` varchar(2) COLLATE utf8_unicode_ci DEFAULT NULL,
`pwd` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`mid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
MariaDB [(none)]> show databases;
MariaDB [(none)]> create database kkdb; // drop database kkdb;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show databases;
MariaDB [(none)]> show engines \G;
MariaDB [(none)]> use kkdb;
Database changed
MariaDB [kkdb]>
MariaDB [kkdb]> create table tblmen (mid int primary key auto_increment, na varchar(20), accnt varchar(20), pwd varchar(20));
MariaDB [kkdb]> describe tblmen;
MariaDB [kkdb]> show tables;
MariaDB [kkdb]> show create table tblmen \G;
*************************** 1. row ***************************
Table: tblmen
Create Table: CREATE TABLE `tblmen` (
`mid` int(11) NOT NULL AUTO_INCREMENT,
`na` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
`accnt` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
`pwd` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`mid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
1 row in set (0.00 sec)
MariaDB [kkdb]> show create table tblmen \G;
*************************** 1. row ***************************
Table: tblmen
Create Table: CREATE TABLE `tblmen` (
`mid` int(11) NOT NULL AUTO_INCREMENT,
`na` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
`accnt` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
`pwd` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`mid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
1 row in set (0.00 sec)
----------------- select insert update delete ----------------------
MariaDB [kkdb]> insert into tblmen values('','kk1','act1','pwd1');
MariaDB [kkdb]> select * from tblmen;
MariaDB [kkdb]> update tblmen set na='kkk1',accnt='actt1',pwd='pwdd1' where mid=1;
MariaDB [kkdb]> select * from tblmen;
MariaDB [kkdb]> delete from tblmen where mid=1;
MariaDB [kkdb]> select * from tblmen;
MariaDB [kkdb]> insert into tblmen values('','kk2','act2','pwd2');
MariaDB [kkdb]> select * into outfile 'tblmen.txt' from tblmen;
# ls /var/lib/mysql/kkdb
... tblmen.txt
# cat /var/lib/mysql/kkdb/tblmen.txt
2 kk2 act2 pwd2
----------- 匯入資料 --------
# vi /var/lib/mysql/kkdb/tblmen.txt // 區隔符號 Tab
3 kk3 act3 pwd3
4 kk4 act4 pwd4
MariaDB [kkdb]> select * from tblmen;
---------- 使用phpMyAdmin -------------
# yum -y install epel-release // 需要 epel套件庫支援才能安裝 phpMyAdmin
# yum -y install phpMyAdmin
# rpm -qa | grep phpMyAdmin
phpMyAdmin-4.4.15.10-3.el7.noarch
# vi /etc/httpd/conf.d/phpMyAdmin.conf
...
<Directory /usr/share/phpMyAdmin/>
AddDefaultCharset UTF-8
<IfModule mod_authz_core.c>
# Apache 2.4
<RequireAny>
Require ip 192.168.1.103 //要連結到 phpMyAdmin 的IP位置
Require ip 127.0.0.1
...
<Directory /usr/share/phpMyAdmin/setup/>
<IfModule mod_authz_core.c>
# Apache 2.4
<RequireAny>
Require ip 192.168.1.103
Require ip 127.0.0.1
...
# systemctl restart httpd.service
登入
使用者名稱:root
密碼:db123456
資料庫-->建立新資料庫-->
kkdb ; utf8_general_ci -->建立
建立資料表-->名稱:tblmen -->欄位數:4 -->執行
名稱 型態 長度值 索引 A_I
mid int PRIMARY ˅
na varchar 10
accnt varchar 10
pwd varchar 10
預覽SQL
CREATE TABLE 'kkdb'.'tblmen' ('mid' INT NOT NULL AUTO_INCREMENT, 'na' VARCHAR(10) NOT NULL, 'accnt' VARCHAR(10) NOT NULL, 'pwd' VARCHAR(10) NOT NULL, PRIMARY KEY ('mid')) ENGINE = InnDB;
-->儲存
------- index.php -----------------------------------------
# vi /var/www/html/index.php
<?php
if ($_POST["act"]=="login"){
$db_link=mysql_connect("localhost","root","db123456");
$db_sel=mysql_select_db("kkdb");
$str_sel="SELECT * FROM `tblmen` WHERE `accnt`= '".$_POST["accnt"]."' AND `pwd`='".$_POST["pwd"]."'";
$result=mysql_query($str_sel);
$data_cnt=mysql_num_rows($result);
if ($data_cnt==1){
header("Location:list.php");
}else{
header("Location:index.php");
}
}
?>
<html>
<head></head>
<body>
<h1 align="center">會員登入</h1>
<form action="" method="post" name="formAdd">
<table align="center">
<tr><td>帳號</td><td><input type="text" name="accnt"></td></tr>
<tr><td>密碼</td><td><input type="text" name="pwd"></td></tr>
<tr><td></td><td>
<input type="hidden" value="login" name="act">
<input type="submit" value="登入" name="ok">
</td></tr>
</table>
</form>
</body>
</html>
------- list.php -----------------------------------------
# vi /var/www/html/list.php
<?php
$db_link=mysql_connect("localhost","root","db123456");
mysql_query("SET NAMES 'utf8'");
$db_sel=mysql_select_db("kkdb");
$str_sel="select * from tblmen";
$result=mysql_query($str_sel);
?>
<html>
<head></he/ad>
<body>
<h1 align="center">學生系統</h1>
<p align="center"><a href="add.php">新增學生資料</a> <a href="query.php">查詢學生資料</a></p>
<table align="center">
<tr><td>編號</td><td>名字</td><td>帳號</td><td>密碼</td></tr>
<?php
while ($tbl_rows=mysql_fetch_assoc($result)){
echo "<tr>";
echo "<td>".$tbl_rows["mid"]."</td>";
echo "<td>".$tbl_rows["na"]."</td>";
echo "<td>".$tbl_rows["accnt"]."</td>";
echo "<td>".$tbl_rows["pwd"]."</td>";
echo "<td><a href='update.php?mid=".$tbl_rows["mid"]."'>修改</a></td>";
echo "<td><a href='delete.php?mid=".$tbl_rows["mid"]."'>刪除</a></td>";
echo "</tr>";
}
?>
</table>
</body>
</html>
------- add.php -----------------------------------------
# vi /var/www/html/add.php
<?php
if ($_POST["act"]=="add"){
$db_link=mysql_connect("localhost","root","db123456");
mysql_query("SET NAMES 'utf8'");
$db_sel=mysql_select_db("kkdb");
$str_insert="INSERT INTO `tblmen`(`mid`, `na`, `accnt`, `pwd`) VALUES ('".$_POST["mid"]."','".$_POST["na"]."','".$_POST["accnt"]."','".$_POST["pwd"]."')";
mysql_query($str_insert);
header("Location:list.php");
}
?>
<html>
<head></head>
<body>
<h1 align="center">新增資料</h1>
<p align="center"><a href="list.php">返回主畫面</a></p>
<form action="" method="post" name="formAdd">
<table align="center">
<tr><td>編號</td><td><input type="text" name="mid"></td></tr>
<tr><td>名字</td><td><input type="text" name="na"></td></tr>
<tr><td>帳號</td><td><input type="text" name="accnt"></td></tr>
<tr><td>密碼</td><td><input type="text" name="pwd"></td></tr>
<tr><td></td><td>
<input type="hidden" value="add" name="act">
<input type="submit" value="新增" name="ok">
<input type="reset" value="清除" name="erase">
</td></tr>
</table>
</form>
</body>
</html>
------- update.php -----------------------------------------
# vi /var/www/html/update.php
<?php
$db_link=mysql_connect("localhost","root","db123456");
mysql_query("SET NAMES 'utf8'");
$db_sel=mysql_select_db("kkdb");
if ($_POST["act"]=="update"){
$str_update="UPDATE `tblmen` SET `na`='".$_POST["na"]."',`accnt`='".$_POST["accnt"]."',`pwd`='".$_POST["pwd"]."' WHERE `mid` ='".$_POST["mid"]."'";
mysql_query($str_update);
header("Location:list.php");
}
$str_sel="SELECT * FROM `tblmen` WHERE `mid`='".$_GET["mid"]."'";
//echo $str_sel;
$result=mysql_query($str_sel);
$tbl_rows=mysql_fetch_assoc($result);
?>
<html>
<head></head>
<body>
<h1 align="center">修改資料</h1>
<p align="center"><a href="list.php">返回主畫面</a></p>
<form action="" method="post" name="formUpdate">
<table align="center">
<tr><td>編號</td><td><input type="text" name="mid" value="<?php echo $tbl_rows["mid"]; ?>" disabled></td></tr>
<tr><td>名字</td><td><input type="text" name="na" value="<?php echo $tbl_rows["na"]; ?>"></td></tr>
<tr><td>帳號</td><td><input type="text" name="accnt" value="<?php echo $tbl_rows["accnt"]; ?>"></td></tr>
<tr><td>密碼</td><td><input type="text" name="pwd" value="<?php echo $tbl_rows["pwd"]; ?>"></td></tr>
<tr><td></td><td>
<input type="hidden" value="<?php echo $tbl_rows["mid"]; ?>" name="mid">
<input type="hidden" value="update" name="act">
<input type="submit" value="更新" name="ok">
<input type="reset" value="清除" name="erase">
</td></tr>
</table>
</form>
</body>
</html>
------- query.php -----------------------------------------
# vi /var/www/html/query.php
<html>
<head></head>
<body>
<h1 align="center">會員查詢</h1>
<p align="center"><a href="list.php">返回主畫面</a></p>
<form action="" method="post" name="formQuery">
<table align="center">
<tr><td>姓名</td><td><input type="text" name="na"></td><td></td><td></td></tr>
<tr><td></td><td>
<input type="hidden" value="query" name="act">
<input type="submit" value="查詢" name="ok">
</td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td></tr>
<tr><td>編號</td><td>姓名</td><td>帳號</td><td>密碼</td></tr>
<?php
if ($_POST["act"]=="query"){
$db_link=mysql_connect("localhost","root","db123456");
mysql_query("SET NAMES 'utf8'");
$db_sel=mysql_select_db("kkdb");
$str_sel="SELECT * FROM `tblmen` WHERE `na` LIKE '%".$_POST["na"]."%'";
//echo $str_sel;
$result=mysql_query($str_sel);
$data_cnt=mysql_num_rows($result);
if ($data_cnt!=0){
while ($tbl_rows=mysql_fetch_assoc($result)){
echo "<tr>";
echo "<td>".$tbl_rows["mid"]."</td>";
echo "<td>".$tbl_rows["na"]."</td>";
echo "<td>".$tbl_rows["accnt"]."</td>";
echo "<td>".$tbl_rows["pwd"]."</td>";
echo "</tr>";
}
}
}
?>
</table>
</form>
</body>
</html>
------- delete.php -----------------------------------------
# vi /var/www/html/delete.php
<?php
$db_link=mysql_connect("localhost","root","db123456");
$db_sel=mysql_select_db("kkdb");
$str_del="DELETE FROM `tblmen` WHERE `mid`='".$_GET["mid"]."'";
$result=mysql_query($str_del);
?>
<html>
<head></head>
<body>
<h1 align="center">資料刪除完畢!</h1>
<p align="center"><a href="list.php">返回主畫面</a></p>
</body>
</html>