MYSQLi 相關函數連結資料庫流程
- 先連結主機與資料庫 mysqli_connect
- 使用 mysqli_query() 函數設定編碼
- 使用 mysqli_query() 函數執行 SQL 語法,SQL語法可分為兩種
- SQL語法若是 insert into, delete, update 三個語法則會變更資料表內容
- SQL語法若是 select 語法,執行 mysqli_query() 後的結果傳遞給變數儲存,接著就可以利用 mysqli_num_rows() 取得查詢後筆數,或者利用 mysqli_fetch_array() 取出每一筆記錄
- 使用 mysqli_free_result() 會將 mysqli_query() 執行結果清除
- 使用 mysqli_close() 函數關閉 MYSQL 伺服器連結
------------------------------------------------------------------------------
<?php
if (!empty($_POST['gra']) && !empty($_POST['cla']) && !empty($_POST['sno']) && !empty($_POST['pwd'])){
$conn=mysqli_connect("localhost","root","db123456","kkdb");
if (mysqli_connect_errno($conn))
die("無法連線!");
mysqli_set_charset($conn,"utf8");
$sql="SELECT * FROM tblstu WHERE gra='".$_POST['gra']."' AND cla='".$_POST['cla']."' AND sno='".$_POST['sno']."' AND pwd='".$_POST['pwd']."'";
echo $sql;
$rslt=mysqli_query($conn,$sql);
$cnt=mysqli_num_rows($rslt);
if ($cnt==1){
header("Location:00.php");
}
else{
header("Location:index.php");
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>電腦作業園</title>
<link href="WebStyle.css" rel="stylesheet" type="text/css">
</head>
<body>
<header>
<h1>同學登入</h1>
<p>線上測驗 討論專區 留言板區 聊天專區 修改帳號</p>
</header>
<article>
<form action="<?php $_SERVER["PHP_SELF"] ?>" method="post">
<table>
<tr>
<td>年級</td>
<td><select name="gra">
<option value="0">請選擇</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
</select>
</td>
<td>班級</td>
<td><select name="cla">
<option value="00">請選擇</option>
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
</select>
</td>
<td>座號</td>
<td><select name="sno">
<option value="00">請選擇</option>
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
</select>
</td>
</tr>
<tr>
<td></td>
<td>
</td>
<td>
<input name="pwd" type="password" required>
</td>
</tr>
<tr>
<td>
<input type="submit" value="登入網站">
</td>
</tr>
<tr>
<td>
</td>
</tr>
</table>
</form>
</article>
<footer>
<p>Copyright © 2019 GuoChin All Rights Reserved</p>
</footer>
</body>
</html>
----------------------------------------------------------------------
# yum -y install httpd
# rpm -qa | grep httpd
httpd-tools-2.4.6-90.el7.centos.x86_64
httpd-2.4.6-90.el7.centos.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.4/
# yum -y install php php-mysql php-devel php-mbstring
# rpm -qa | grep php
php-common-5.4.16-46.el7.x86_64
php-pdo-5.4.16-46.el7.x86_64
php-mysql-5.4.16-46.el7.x86_64
php-mbstring-5.4.16-46.el7.x86_64
php-cli-5.4.16-46.el7.x86_64
php-devel-5.4.16-46.el7.x86_64
php-5.4.16-46.el7.x86_64
# vi /etc/php.ini
...
display_errors = On
...
# systemctl restart httpd.service
...
display_errors = On
...
# systemctl restart httpd.service
# yum -y install mariadb-server
mariadb-5.5.64-1.el7.x86_64
mariadb-server-5.5.64-1.el7.x86_64
mariadb-libs-5.5.64-1.el7.x86_64
# systemctl start mariadb.service
# systemctl enable mariadb.service
# systemctl status mariadb.service
# mysqladmin -u root password db123456
# mysql -u root -p
Enter password:
MariaDB [(none)]> create database kkdb character set='utf8' collate='utf8_unicode_ci';
MariaDB [(none)]> use kkdb;
MariaDB [kkdb]> create table tblstu (sid int primary key auto_increment, gra varchar(1), cla varchar(2), sno varchar(2), na varchar(10), pwd varchar(10));
MariaDB [kkdb]> show create table tblstu \G;
*************************** 1. row ***************************
Table: tblstu
Create Table: CREATE TABLE `tblstu` (
`sid` int(11) NOT NULL AUTO_INCREMENT,
`gra` varchar(1) COLLATE utf8_unicode_ci DEFAULT NULL,
`cla` varchar(2) COLLATE utf8_unicode_ci DEFAULT NULL,
`sno` varchar(2) COLLATE utf8_unicode_ci DEFAULT NULL,
`na` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL,
`pwd` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`sid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
---------------------------------------
<?php
if (!empty($_POST['gra']) && !empty($_POST['cla']) && !empty($_POST['sno']) && !empty($_POST['na']) && !empty($_POST['pwd'])){
$conn=mysqli_connect("localhost","root","db123456","kkdb");
if (mysqli_connect_errno($conn))
die("無法連線!");
mysqli_set_charset($conn,"utf8");
$sql="INSERT tblstu(gra,cla,sno,na,pwd)VALUES('{$_POST['gra']}','{$_POST['cla']}','{$_POST['sno']}','{$_POST['na']}','{$_POST['pwd']}')";
mysqli_query($conn,$sql);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>電腦作業園</title>
<link href="WebStyle.css" rel="stylesheet" type="text/css">
</head>
<body>
<header>
<h1>同學登入</h1>
<p>線上測驗 討論專區 留言板區 聊天專區 修改帳號</p>
</header>
-------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------
MariaDB [kkdb]> create table tblstu (sid int primary key auto_increment, gra varchar(1), cla varchar(2), sno varchar(2), na varchar(10), pwd varchar(10));
MariaDB [kkdb]> show create table tblstu \G;
*************************** 1. row ***************************
Table: tblstu
Create Table: CREATE TABLE `tblstu` (
`sid` int(11) NOT NULL AUTO_INCREMENT,
`gra` varchar(1) COLLATE utf8_unicode_ci DEFAULT NULL,
`cla` varchar(2) COLLATE utf8_unicode_ci DEFAULT NULL,
`sno` varchar(2) COLLATE utf8_unicode_ci DEFAULT NULL,
`na` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL,
`pwd` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`sid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
<?php
if (!empty($_POST['gra']) && !empty($_POST['cla']) && !empty($_POST['sno']) && !empty($_POST['na']) && !empty($_POST['pwd'])){
$conn=mysqli_connect("localhost","root","db123456","kkdb");
if (mysqli_connect_errno($conn))
die("無法連線!");
mysqli_set_charset($conn,"utf8");
$sql="INSERT tblstu(gra,cla,sno,na,pwd)VALUES('{$_POST['gra']}','{$_POST['cla']}','{$_POST['sno']}','{$_POST['na']}','{$_POST['pwd']}')";
mysqli_query($conn,$sql);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>電腦作業園</title>
<link href="WebStyle.css" rel="stylesheet" type="text/css">
</head>
<body>
<header>
<h1>同學登入</h1>
<p>線上測驗 討論專區 留言板區 聊天專區 修改帳號</p>
</header>
<article>
<form action="<?php $_SERVER["PHP_SELF"] ?>" method="post">
<table>
<tr>
<td>年級</td>
<td><select name="gra">
<option value="0">請選擇</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
</select>
</td>
<td>班級</td>
<td><select name="cla">
<option value="00">請選擇</option>
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
</select>
</td>
<td>座號</td>
<td><select name="sno">
<option value="00">請選擇</option>
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
</select>
</td>
</tr>
<tr>
<td>姓名</td>
<td>
<input name="na" type="text" required>
</td>
<td>密碼</td>
<td>
<input name="pwd" type="password" required>
</td>
</tr>
<tr>
<td>
<input type="submit" value="登錄資料">
</td>
</tr>
<tr>
<td>
</td>
</tr>
</table>
</form>
</article>
<footer>
<p>Copyright © 2019 GuoChin All Rights Reserved</p>
</footer>
</body>
</html>
-------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------
MariaDB [kkdb]> create table tblmen (mid int primary key auto_increment, na varchar(20), accnt varchar(20), pwd varchar(20));
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
MariaDB [kkdb]> insert into tblmen values('','馬英九','act1','pwd1');
MariaDB [kkdb]> select * from tblmen;
+-----+-----------+-------+------+
| mid | na | accnt | pwd |
+-----+-----------+-------+------+
| 1 | 馬英九 | act1 | pwd1 |
+-----+-----------+-------+------+
# vi /var/www/html/t1.php
<?php
$conn=mysqli_connect("localhost","root","db123456","kkdb");
if (mysqli_connect_errno($conn))
die("無法連線!");
echo mysqli_connect_errno($conn); //0表示沒有錯誤
mysqli_set_charset($conn,"utf8");
$result=mysqli_query($conn,"select * from tblmen");
$row=mysqli_fetch_array($result);
echo $row[1];
echo $row['na'];
echo mysqli_num_rows($result); //取得查詢結果的筆數
?>
# ls -Z /var/www
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 html
# ls -Z /var/www/html
-rw-r--r--. root root system_u:object_r:httpd_sys_content_t:s0 t1.php
---------------------------------------------------------------------------------
------------------------------------------------------$conn=mysqli_connect("localhost","root","db123456","kkdb");
if (mysqli_connect_errno($conn))
die("無法連線!");
echo mysqli_connect_errno($conn); //0表示沒有錯誤
mysqli_set_charset($conn,"utf8");
$result=mysqli_query($conn,"select * from tblmen");
$row=mysqli_fetch_array($result);
echo $row[1];
echo $row['na'];
echo mysqli_num_rows($result); //取得查詢結果的筆數
?>
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 html
# ls -Z /var/www/html
-rw-r--r--. root root system_u:object_r:httpd_sys_content_t:s0 t1.php
---------------------------------------------------------------------------------
# vi /etc/php.ini
display_errors = On
---------------------------------
# yum install php-mysql -y
# systemctl restart httpd
$conn=mysqli_connect("localhost","root","db123456");
if($conn){
echo"ok";
}else{
echo"error";
}
?>
# vi /etc/selinux/config
...
SELINUX=disabled
...
# shutdown now
# sestatus
SELinux status: disabled
安裝apache
# yum install httpd
開啟防火牆 Selinux
# firewall-cmd --zone=public --add-port=80/tcp --permanent
# firewall-cmd --reload
開啟SELINUX
getsebool -a | grep 'httpd'
# setsebool httpd_can_network_connect on
# setsebool httpd_can_network_connect_db on
# setsebool httpd_can_sendmail on
要讓httpd寫入的目錄,例如上傳目錄
# chcon -t httpd_sys_rw_content_t /path/to/upload
要讓httpd讀取的目錄,例如網頁目錄 (預設的/var/www/html已設定)
# chcon -R -t httpd_sys_content_t /path/to/html
安裝PHP7[1]
如果你已有安裝其他版本的PHP,請先移除它們
# yum remove php*
# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
# yum install php70w php70w-opcache
# yum install php70w-mysqlnd php70w-mbstring php70w-gd
# yum install php70w-pecl-imagick
設定 vi /etc/php.ini
; 時區
date.timezone = "Asia/Taipei"
; Session 時間改一天
session.gc_maxlifetime = 86400
; 記憶體,預設128M
memory_limit = 1024M
; 上傳大小限制和時間
post_max_size = 20M
upload_max_filesize = 20M
max_input_time = 120
default_socket_timeout = 600
安裝mariadb10
建立 yum repository
# vim /etc/yum.repos.d/MariaDB.repo
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
# yum install MariaDB-server MariaDB-client
啟動
# systemctl enable httpd
# systemctl enable mariadb
# systemctl start mariadb
# systemctl start httpd
你的檔案放在 /var/www/html中
開你的網頁 http://x.x.x.x/ 來測試能不能看到歡迎畫面,我發現這頁還是responsive web design。
如果出現forbidden代表selinux或檔案權限問題,如果等很久沒頁面是防火牆問題。
其他的問題原因太複雜得CASE by CASE
HomeMySQL查詢已安裝的 MySQL / MariaDB 版本
查詢已安裝的 MySQL / MariaDB 版本 Sam Tang 23 March 2017 MySQL No Comments
由於各個 MySQL 的版本都略有不同,而且又有 MaridDB 或 Percona 等分支版本, 很多時系統管理員都需要查詢系統內的 MySQL 版本,要查詢系統內的 MySQL 版本,以下列出幾種查詢 MySQL 版本的方法。
CLI
在 CLI 查詢 MySQL 版本十分簡單,用 mysql 指令便可以, 而且不用登入 MySQL:
$ mysql -V
執行後會輸出 MySQL 的版本, 以下是在 CentOS 用 yum 安裝 MariaDB 版本例子:
mysql Ver 15.1 Distrib 5.5.52-MariaDB, for Linux (x86_64) using readline 5.1
除了用 mysql 指令外,如果 MySQL 是用系統的套件管理工具安裝,也可以用套件管理工具查詢, 但這個方法的缺點是, 必須要知道 MySQL 版本的分支, 例如是 MySQL, MariaDB 或 Percona, 以下是查詢 MariaDB 版本的例子:
# rpm -qa | grep mariadb
執行後會有類似下的輸出:
mariadb-libs-5.5.52-1.el7.x86_64
mariadb-5.5.52-1.el7.x86_64
mariadb-server-5.5.52-1.el7.x86_64
MySQL Client
MariaDB [(none)]> select version();
沒有留言:
張貼留言