Common code for PHP connections and reading and writing to MySQL databases

Source: Internet
Author: User
Tags php write

Here I summarize the common PHP connection MySQL database and read Write database method, I hope to help you, of course, as a review of my own summary.

1. To better set up a data connection, the values involved in the data connection are typically defined as variables.

$mysql _server_name // change to your MySQL database server $mysql _username // change to your MySQL database user name $mysql _password // change to your MySQL database password $mysql _database // change to your MySQL database name

You can also put the above variables in a file, you can make other file calls at any time.

For example, put the above in the following: Db_config.php is called directly on other pages that need to use the database.

Calling code: Require ("db_config.php");

2. Connect to the database

$conn=mysql_connect($mysql _server_name,$mysql _username,$mysql _password) or die("Error connecting");//connecting to a database mysql_query("Set names ' UTF8 '");//the database output encoding should be consistent with your database encoding. Nanchang Website Construction company hundred Constant network PHP engineer proposed to use UTF-8 International standard Code. mysql_select_db($mysql _database);//Open Database $sql= "SELECT * FROM News";//SQL statements $result=mysql_query($sql,$conn);//Enquiry

3. Read the contents of the table, here we use the while, you can use for or other depending on the situation.

 while ($rowmysql_fetch_array($resultecho// typesetting code  echo$row[' Topic ']. "<br/>"echo// typesetting code }

4.php write database, MySQL data write

$conn=mysql_connect($mysql _server_name,$mysql _username,$mysql _password);//connecting to a database mysql_query("Set names ' UTF8 '");//Database Output Encoding mysql_select_db($mysql _database);//Open Database $sql= "INSERT into messageboard (topic,content,enabled,date) VALUES ('$Topic‘,‘$Content', ' 1 ', ' 2011-01-12 ') "; mysql_query($sql); Mysql_close();//Close MySQL Connection

Common code for PHP connections and reading and writing to MySQL databases

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.