15、sql語句集,Linux 下PHP查詢mysql

來源:互聯網
上載者:User

標籤:

一、mysql 建立資料庫

mysql密碼,預設沒有

如果想更改的話, mysqladmin  -uroot   password  root123

登入   : [[email protected] root]#   mysql    -uroot    -proot123

1、建立book資料庫

mysql>   create   database   book;
Query OK, 1 row affected (0.08 sec)

 

2、授權給 lili 使用者

mysql>  grant   all   on   book.*   to   [email protected]   identified   by   "lili123";
Query OK, 0 rows affected (0.06 sec)

mysql> exit
Bye

 

3、lili登入

建立lili使用者

[[email protected] html]#   mysql   -u   lili    -p
Enter password:       (這裡輸密碼)
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5 to server version: 3.23.54

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the buffer.

mysql>

 

4、建立book表,添加資料

mysql> use  book;    (先確定要操作的資料庫)
Database changed

 

5、建立book表

mysql> create   table   book(
    -> id   int   not   null,
    -> name   char(20)   not   null,
    -> primary   key(id)
    -> );
Query OK, 0 rows affected (0.08 sec)

 

6、添加資料

mysql>  insert   into   book   values

    -> (1,"語文"),
    -> (2,"數學"),
    -> (3,"英語");
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

 

(注意:上面操作也可以用sql語句集來執行:   (好處是,去別的機子也可以執行sql語句集 就行,不用從頭打過)

[[email protected] html]#  cat  >  lili.sql
use   book;
create   table   book(
    id   int   not   null,
    name   char(20)   not  null,
    primary   key(id)
    );
insert   into  book  values
     (1,"語文",),
     (2,"數學"),
     (3,"英語");

[[email protected] html]# vi  root.sql
create   database   book;
grant   all   on   book.*   to   [email protected]   identified   by   "lili123" ;

[[email protected] html]# mysql   -u   root   -p   <   root.sql
Enter password:

[[email protected] html]# mysql   -u   lili   -p   <   lili.sql
Enter password:

 

 

7、查詢表

mysql> select   *   from   book;
+----+--------+
| id | name   |
+----+--------+
|  1 | 語文 |
|  2 | 數學 |
|  3 | 英語 |
+----+--------+

 

退出mysql
mysql> exit;
Bye

 

二、在 /var/www/html  中 建立index.php

[[email protected] html]#  vi    index.php

<h1><?php   mysql_connect(NULL,"lili","lili123","localhost");   mysql_selectdb("book");   $sql="select * from book";   $res=mysql_query($sql);   while($row=mysql_fetch_row($res)){      print "$row[0] $row[1]<br/>";              }      $a=array("陽光","沙灘","海浪","仙人掌");      for($i=0;$i<4;$i++){        print "$a[$i]<br/>";              }?></h1>

 

在瀏覽器輸入  http://192.168.170.3/  ,  結果如下:

 

如果想要網格線

<html><title></title><body><h1><?php   mysql_connect(NULL,"lili","lili123","localhost");   mysql_selectdb("book");   $sql="select * from book";   $res=mysql_query($sql);   print (‘<table border=1>‘);   while($row=mysql_fetch_row($res)){     print (‘<tr>‘);     for($i=0;$i<3;$i++){        print "<td>$row[$i]</td>";      }    print (‘</tr>‘);              }     print (‘</table>‘);      $a=array("陽光","沙灘","海浪","仙人掌");       for($i=0;$i<4;$i++){              print "$a[$i]<br/>";                 }?></h1></body></html>

效果如下:

 

 

15、sql語句集,Linux 下PHP查詢mysql

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.