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