MySQL & amp; PHP Study Notes [5.05.04.19], mysqlserver

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.