15. SQL statement set. in Linux, PHP queries mysql and sqlmysql.
I. Create a database in mysql
Mysql password, no by default
If you want to change the value, mysqladmin-uroot password root123
Logon: [root @ localhost root] # mysql-uroot-proot123
1. Create a book Database
Mysql> create database book;
Query OK, 1 row affected (0.08 sec)
2. Authorize lili user
Mysql> grant all on book. * to lili @ localhost identified by "lili123 ";
Query OK, 0 rows affected (0.06 sec)
Mysql> exit
Bye
3. lili Logon
Create a lili user
[Root @ localhost html] # mysql-u lili-p
Enter password: (Enter the password here)
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. Create a book table and add data
Mysql> use book; (first, determine the database to operate)
Database changed
5. Create a book table
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. add data
Mysql> insert into book values
-> (1, "Chinese "),
-> (2, "Mathematics "),
-> (3, "English ");
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
(Note: The preceding operation can also be executed using an SQL statement set: (the advantage is that you can also execute an SQL statement set on another machine without having to start from scratch)
[Root @ localhost 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, "Chinese ",),
(2, "Mathematics "),
(3, "English ");
[Root @ localhost html] # vi root. SQL
Create database book;
Grant all on book. * to lili @ localhost identified by "lili123 ";
[Root @ localhost html] # mysql-u root-p <root. SQL
Enter password:
[Root @ localhost html] # mysql-u lili-p <lili. SQL
Enter password:
)
7. query a table
Mysql> select * from book;
+ ---- + -------- +
| Id | name |
+ ---- + -------- +
| 1 | language |
| 2 | mathematics |
| 3 | English |
+ ---- + -------- +
Exit mysql
Mysql> exit;
Bye
If you want to query through command lines
[Root @ localhost root] # echo "select * from book;" | mysql-u lili-p book
Enter password:
Id name
1 Language
2. Mathematics
3 English
2. Create index. php in/var/www/html
[Root @ localhost 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 ("Sunshine", "beach", "waves", "Cactus"); for ($ I = 0; $ I <4; $ I ++) {print "$ a [$ I] <br/>" ;}?> </H1>
Enter http: // 192.168.170.3/in the browser. The result is as follows:
<Html> <title> </title> <body>
The effect is as follows: