Now that you have read this article, you must know what PHP and MySQL are like. But why are you still reading this article? Maybe 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 haven't touched it for a while, 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 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