MYSQL

  • Mysql is the name of a database
  • A good database to work with PHP
  • One of the problems we talked about earlier is that the front end asks for data from the back end, and the back end just queries the data in the database and returns it to the front end
  • Let’s talk about using PHP to manipulate databases

MySQL is the most popular Relational Database Management System (a brief introduction to non-relational databases) Relational Database Management System (Relational Database Management System) data in the form of tables, each behavior of a variety of record names, many rows and columns formed a form Several forms form the DATABASE primary key: The primary key is unique. A table can contain only one primary key. You can query data using primary keys.

The data type of the database

Numeric types Date and time types String type

Operating database

  • Before are simple understanding of the database, do not need to recite all down
  • We just need to know
  • The next step is to use PHP to link to mysql database to add, delete, change and check data
  • To operate a database, you need a MYSQL SQL statement in addition to the PHP syntax
  • Steps to operate a database using PHP

Create a link with the database. 2. Operate the database using SQL statements. 3

Establish a link to the database

  • In PHP we use the mysql_connect() method to establish a link to the database

      
 $link will return a link message
  $link = mysql_connect('IP address'.'Database username'.'Database password'); 
? >
Copy the code
  • After we have the link information $link, we can continue to operate the database

Determine which libraries to operate on

  • We just set up a link with the database, and we need to decide which library to operate on

      
 The next step is to determine which library you want to operate
  mysql_select_db('Name of the library you want to operate on'.$link);
? >
Copy the code

Execute SQL statements to operate the database

  • The next step is to use SQL statements to add, delete, change and check the database

      
 SQL > alter database
  $res = mysql_query('SQL statement you want to execute');      
? >
Copy the code
  • Here’s one caveat:
  • What we get is a processing message that we don’t understand
  • You need to use the mysql_fetch_row | | mysql_fetch_assoc analytic results can be read

Close links

  • We’d better close the database link when we’re done

      
  mysql_close($conn);
? >
Copy the code

Complete the steps

  • So let’s write down the whole operation

      
 $conn = mysql_connect('localhost'.'root'.'root');
 mysql_select_db('test1913');
 $res = mysql_query('SELECT * FROM `student`');
 $row = mysql_fetch_assoc($res);
 mysql_close($conn);
​
 print_r($row);
? >
Copy the code

Common SQL statements

  • What did you say about how to operate the database
  • Now let’s look at some common SQL statements used when operating a database
  • We rely on these SQL statements to perform database operations

check

  • The query

      
 Select * from student
 $sql = 'SELECT * FROM `student`';
 
 Select * from student where gender = male
 $sql = SELECT * FROM 'student' WHERE 'gender' = 'male';
 
 Select * from student where age > 18
 $sql = 'SELECT * FROM `student` WHERE `age`>18';
 
 Select * from student where age > 18 and gender = male
 $sql = SELECT * FROM 'student' WHERE 'age' >18 AND 'gender' = 'male';
​
 Select * from student where age < 22 or age > 28
 $sql = 'SELECT * FROM `student` WHERE `age`<22 OR `age`>28';
​
 Select * from student; select * from student
 $sql = 'SELECT * FROM `student` LIMIT 0, 10';
 
 Filter out the data according to the criteria before paging query
 Select * from table where age>18 and gender = male
 $sql = SELECT * FROM 'student' WHERE 'age' >18 AND 'gender' = 'male' LIMIT 10, 10';
​
 Fuzzy query for query table
 Select * from table where name = '3'
 $sql = SELECT * FROM 'student' WHERE 'name' = '% % %';
​
 Select * from a column in ascending or descending order
 $sql = 'SELECT * FROM `student` ORDER BY `age` ASC';
 $sql = 'SELECT * FROM `student` ORDER BY `age` DESC';
? >
Copy the code

increase

  • Increase the statement

      
 Mysql database increment_primary key; mysql database increment_primary key
 $sql = 'INSERT INTO 'VALUES' (null, 18, 99, 99)';
 
 Insert data for fixed keys, others with default values
 $sql = 'INSERT INTO 'score' VALUES(' name ', 'age');
? >
Copy the code

delete

  • Delete statements

      
 Select * from table where id = 100
 $sql = 'DELETE FROM `student` WHERE `id`=100';
​
 Select * from table where name = 'zhang3'
 $sql = 'DELETE 'FROM' student 'WHERE' name '=' c '
? >
Copy the code

change

  • Modify the statement

      
 Alter table 100 alter table 100 alter table 100
 $sql = UPDATE 'student' WHERE 'id' =100 and 'age' =1
 
 Add some content to all data when updating data
 $sql = 'UPDATE `student` SET `age`=age+1'
? >
Copy the code

More recommended

  • JavaScript Learning Notes (23) – Server PHP
  • JavaScript Learning Notes (22) — Prototypes and prototype chains
  • JavaScript Learning Notes (21) — constructors
  • DOM animation effects