Common Php code for connecting to, reading, and writing to the mysql database

Source: Internet
Author: User
Tags what php
Now that you have read this article, you must know what PHP and MySQL are like. But why are you still reading this article? It may have been because you used to copy and paste some code and didn't really understand the meaning of the code. you may have understood it before, but now that you have read this article, it means you must know what PHP and MySQL are like. But why are you still reading this article? It may be that you used to copy and paste some code and didn't really understand the meaning of the code; or you may have understood it before, but like me, you have not been in touch for a while and are unfamiliar with it; or, someone asks you a simple question like this, and you are already waiting to answer the question. you just found this article by searching on the Internet, so I recommend it to someone who is there...

In any case, I have summarized the commonly used methods for connecting to the MySQL database using PHP and reading and writing data to the database. I hope this will help you. of course it is also a review and summary of my own.

1. to better set up data connections, the values involved in data connections are generally defined as variables.

$ Mysql_server_name = 'localhost'; // change it to your mysql database server $ mysql_username = 'root'; // change it to your mysql database username $ mysql_password = '123 '; // change your mysql database password $ mysql_database = 'mydb'; // change it to your mysql database name

You can also put the preceding variables in one file so that other files can be called at any time.

For example, if you place the preceding content in db_config.php, you can directly call it on other pages that require databases.

Call code: require ("db_config.php ");

2. connect to the database

$ Conn = mysql_connect ($ mysql_server_name, $ mysql_username, $ mysql_password) or die ("error connecting"); // connect to the database mysql_query ("set names 'utf8 '"); // The database output encoding must be consistent with your database encoding. nanchang site construction company baiheng Network PHP engineers are recommended to use UTF-8 international standard code. mysql_select_db ($ mysql_database); // open the database $ SQL = "select * from news"; // SQL statement $ result = mysql_query ($ SQL, $ conn); // query

3. read the content in the table. here we use while. you can use for or other.

While ($ row = mysql_fetch_array ($ result) {echo"

"; // Typographical code echo $ row ['topic ']."
"; Echo"

"; // Typographical code}

4. php writes data to the database and Mysql writes data.

$ Conn = mysql_connect ($ mysql_server_name, $ mysql_username, $ mysql_password); // connect to the database mysql_query ("set names 'utf8 '"); // database output code mysql_select_db ($ mysql_database); // open the database $ SQL = "insert into messageboard (Topic, Content, Enabled, Date) values ('$ Topic ', '$ content', '1', '1970-01-12') "; mysql_query ($ SQL); mysql_close (); // Close the MySQL connection

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.