The specific code is as follows:
<?php //Set the character setheader('Content-Type: text/html; charset=utf8'); //Connect the database$link = mysql_connect("localhost","root","123456"); if(!$link){ echo 'Database connection failed...<br>'; exit(-1); }else{ echo "The database connection is successful...<br>"; } //Select a database as the default databasemysql_select_db('php_sjk'); //Execute SQL insertion statement$sql = "INSERT INTO zh VALUES (NULL,'Wang Jie','123456')"; $cr = mysql_query($sql); //Judge whether the insertion is successfulif(!$cr){ echo "Data insertion failed.<br>"; }else{ echo "Data insertion successfully.<br>"; } //Execute SQL query statement$sql = "select * from zh"; $cr = mysql_query($sql); //Get the returned result set and loop the outputecho "Data Display: <br><br><br>"; while($a = mysql_fetch_row($cr)){ echo "{$a[0]}——{$a[1]}——{$a[2]}<br>"; } //Close the database connectionmysql_close($link); ?>
I will introduce so much to you about PHP connecting to MYSQL database code. Everyone is welcome to collect it. Thank you!