Now do the project need PHP to connect MySQL database, although learned before, but now basically forget, and then by looking for relevant information to find a solution, the following small series of specific methods to share in the cloud Habitat Community Platform for everyone to learn.
The specific code looks like this:
<?php
//Set header for character set
(' content-type:text/html Charset=utf8 ');
Connection database
$link = mysql_connect ("localhost", "root", "123456");
if (! $link) {
echo ' database connection failed ...<br> ';
Exit ( -1);
} else{
echo "database connection succeeded ...<br>";
}
Select a database as the default database
mysql_select_db (' php_sjk ');
Execute SQL INSERT statement
$sql = "insert into en VALUES (NULL, ' Wang ', ' 123456 ')";
$CR = mysql_query ($sql);
Determines whether the insert success if
(! $cr) {
echo data insertion failed. <br> ";
} else{
echo "Data insertion succeeded. <br> ";
}
Execute SQL query statement
$sql = "select * from en";
$CR = mysql_query ($sql);
Gets the result set returned and loops out the
echo "data display:<br><br><br>";
while ($a = mysql_fetch_row ($CR)) {
echo {$a [0]}--{$a [1]}--{$a [2]}<br>;
}
Close the database connection
mysql_close ($link);
? >
About PHP connection MySQL database code to introduce so many, welcome everyone collection, thank you!