MySQL & PHP Study Notes [5.05.04.19], mysqlserver
I just started to learn MySQL and PHP, and I want to share some of my learned knowledge with you.
1. Use PHP to connect to the MySQL database and use the following methods to connect to the database (the premise is that your environment has been set up)
$ Host = "localhost ";
$ User = "root ";
$ Password = "123456 ";
$ Database = "lyz ";
$ Port = 3306;
$ Connection = new mysqli ($ host, $ user, $ password, $ database, $ port );
2. view the connection result. You can use the mysqli_connect_errno () function to check whether an error is returned during the connection process.
If (mysqli_connect_errno ()){
Echo "<p> connection failed". mysqli_connect_error (). "</p> \ n ";
} Else {
Echo "<p> connection successful </p> \ n ";
}
3. Execute SQL statements
After connecting to the database, you need to query and modify the database. The following statement queries a database table test.
$ Result = $ connection-> query ("select * from test ");
You can use the fetch_fields () function to obtain the table fields and view the Code directly.
$ Num = $ result-> field_count;
$ Info = $ result-> fetch_fields ();
Echo "<p> table name is:". $ info [0]-> table. "</p> ";
For ($ I = 0; $ I <$ num; $ I ++ ){
Echo $ info [$ I]-> name. "\ t ";
}
4. Finally, we can output the content of the test table.
$ Rs = $ result-> fetch_row ();
While ($ rs ){
Echo "<p>". $ rs [0]. "\ t". $ rs [1]. "</p> ";
$ Rs = $ result-> fetch_row ();
}
If ($ result ){
Echo "<p> Number of records:". $ result-> num_rows. "</p> ";
Echo "<p> Number of fields:". $ result-> field_count. "</p> ";
}
$ Result-> close ();
5. Insert data to Table test
$ SQL = "insert into test (B, a) values (?,?) ";
$ Stmt = $ connection-> prepare ($ SQL );
$ Bv = 2;
$ Av = 'a ';
$ Stmt-> bind_param ("is", $ bv, $ av );
$ Stmt-> execute ();
$ Stmt-> close ();
$ Connection-> close ();
OK. The above is the basic operation for MySQL table test-query and add function.