create table 表格名 (
欄位名 資料類型 (長度),
欄位名 資料類型 (長度),
欄位名 資料類型 (長度),
...
primary key (欄位名)
);
# mysql -u username -p Mariadb > use userdb; Mariadb [userdb]> create table `userinfo` ( `uid` int auto_increment not null, `login_name` varchar(40) not null, `login_pass` varchar(64) not null, `realname` varchar(40) not null, `u_email` varchar(100) null, `u_bday` date not null, `u_sex` tinyint not null default 0, `u_active` tinyint not null default 0, `u_regd` timestamp(6) not null, primary key (uid) ) character set utf8 collate utf8_unicode_ci ; Mariadb [userdb]> show tables; Mariadb [userdb]> describe userinfo;
# mysql -u username -p Mariadb > use userdb; Mariadb [userdb]> insert into userinfo (login_name,login_pass,realname,u_bday,u_sex) values ('a030cxxx','1234','鳥哥','197101',1); Mariadb [userdb]> insert into userinfo (login_name,login_pass,realname,u_bday,u_sex) values ('a030c101','1234','曾崇勝','199101',1); Mariadb [userdb]> select * from userinfo; +-----+------------+------------+-----------+---------+------------+-------+----------+----------------------------+ | uid | login_name | login_pass | realname | u_email | u_bday | u_sex | u_active | u_regd | +-----+------------+------------+-----------+---------+------------+-------+----------+----------------------------+ | 1 | a030cxxx | 1234 | 鳥哥 | NULL | 0000-00-00 | 1 | 0 | 2017-03-13 17:04:50.964400 | | 2 | a030c101 | 1234 | 曾崇勝 | NULL | 0000-00-00 | 1 | 0 | 2017-03-13 17:06:21.609723 | +-----+------------+------------+-----------+---------+------------+-------+----------+----------------------------+
$ vim index.php <!doctype html> <html> <head> <meta charset='utf-8' /> </head> <body> <?php $db_link = mysql_connect('localhost', 'username', 'userpw'); if ( ! $db_link ) { echo "資料連結失敗了!\n"; } else { echo "資料連結OK\n"; } ?> </body> </html>然後來到你的瀏覽器,直接輸入: http://your_hostname/~yourname 看看執行的成果是否為 OK!如果不是的話, 請到 /var/log/httpd/error_log 查看原因為何!相當重要!
$ vim index.php <!doctype html> <html> <head> <meta charset='utf-8' /> </head> <body> <?php $db_link = mysql_connect('localhost', 'username', 'userpw'); if ( ! $db_link ) { echo "資料連結失敗了!\n"; die; } $db_sel = mysql_select_db ('userdb', $db_link); if ( ! $db_sel ) { echo "資料庫選取失敗了!\n"; } else { echo "資料庫選取OK\n"; } ?> </body> </html>再次到瀏覽去去檢查看看,是否出現了資料庫選取正常的字樣!?相當重要!
$ vim index.php <!doctype html> <html> <head> <meta charset='utf-8' /> </head> <body> <?php $db_link = mysql_connect('localhost', 'username', 'userpw'); if ( ! $db_link ) { echo "資料連結失敗了!\n"; die; } $db_sel = mysql_select_db ('userdb', $db_link); if ( ! $db_sel ) { echo "資料庫選取失敗了!\n"; die; } $result = mysql_query ('select * from userinfo', $db_link ); $db_line = mysql_num_rows($result); echo "總資料數:" . $db_line ; ?> </body> </html>同樣的,透過瀏覽器去瞧一瞧是否有正確的輸出資料數量?
$ vim index.php <?php include ('include/config.php'); ?> <!doctype html> <html> <head> <meta charset='utf-8' /> </head> <body> <?php $result = mysql_query ('select * from userinfo', $db_link ); $db_line = mysql_num_rows($result); echo "總資料數:" . $db_line ; ?> </body> </html>
$ vim listuser.php <?php include ('include/config.php'); ?> <!doctype html> <html> <head> <meta charset='utf-8' /> </head> <body> <?php $result = mysql_query ('select uid, login_name, realname, u_email, u_bday, u_sex, u_regd, u_active from userinfo', $db_link ); $db_line = mysql_num_rows($result); echo "總資料數:" . $db_line ; echo "<br /><br />"; while ( $row = mysql_fetch_row($result) ) { echo $row[0] . $row[1] . $row[2] . $row[3] . $row[4] . $row[5] . $row[6] . $row[7] . "<br />"; } ?> </body> </html>
$ vim listuser2.php <?php include ('include/config.php'); ?> <!doctype html> <html> <head> <meta charset='utf-8' /> </head> <body> <?php $result = mysql_query ('select uid, login_name, realname, u_email, u_bday, u_sex, u_regd, u_active from userinfo', $db_link ); $db_line = mysql_num_rows($result); echo "總資料數:" . $db_line ; echo "<br /><br />"; while ( $row_res = mysql_fetch_row($result) ) { foreach ( $row_res as $row ){ echo $row; } echo "<br />"; } ?> </body> </html>
$ cd ~/www $ mkdir include $ cd ~/www/include $ wget https://github.com/twbs/bootstrap/releases/download/v3.3.7/bootstrap-3.3.7-dist.zip $ unzip bootstrap-3.3.7-dist.zip $ vim ../listuser.php <!doctype html> <html> <head> <meta charset='utf-8' /> <link rel="stylesheet" href="/~student/include/bootstrap-3.3.7-dist/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <script src="/~student/include/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script> </head> <body>
<table class='table table-bordered table-striped table-hover' style='width: 800px; '>