: This article mainly introduces the PHP connection to the MYSQL database instance code. if you are interested in the PHP Tutorial, please refer to it. The current project requires php to connect to the mysql database. although I have learned it before, I almost forgot it now. then I found a solution by searching for relevant information, the following small series share the specific methods on this site platform for everyone to learn.
The code is as follows:
<? Php // set the character set header ('content-Type: text/html; charset = utf8'); // connect to the database $ link = mysql_connect ("localhost", "root ", "123456"); if (! $ Link) {echo 'database connection failed...
'; Exit (-1);} else {echo "database connection successful...
";}// Select a database as the default database mysql_select_db ('php _ sjk'); // execute the SQL INSERT statement $ SQL =" INSERT INTO zh VALUES (NULL, 'Wang jie', '000000') "; $ cr = mysql_query ($ SQL); // you can determine whether the data is successfully inserted if (! $ Cr) {echo "data insertion failed.
";} Else {echo" data is inserted successfully.
";}// Run the SQL query statement $ SQL =" select * from zh "; $ cr = mysql_query ($ SQL); // Obtain the returned result set, and cyclically output echo "data display:
"; While ($ a = mysql_fetch_row ($ cr) {echo" {$ a [0]} -- {$ a [1]} -- {$ a [2]}
";}// Close the database connection mysql_close ($ link);?>
The code for connecting PHP to the MYSQL database is described so much. you are welcome to add it to your favorites. thank you!
The above introduces the PHP connection to the MYSQL database instance code, including the content, hope to be helpful to friends who are interested in the PHP Tutorial.