MySQL connection operation
Establish a connection
$con = mysql_connect (' localhost ', ' root ', ' 123456 ');
Determine if the connection is successful
if ($con) {
Die (' Connection failed! '). Mysql_error ());
Exit
}
Select a character Set
Mysql_set_charset (' UTF8 ', $con);
Select Database
mysql_select_db (' Test ', $con);
$sql = ' .......... '; '; '; ';
Execute SQL statement
$result = mysql_query ($sql, $con);
If the insert operation
Mysql_affected_rows () Gets the number of rows affected
MYSQL_INSERT_ID () Gets the newly inserted ID
If the query operation
while ($row = Mysql_fetch_assoc ($result)) {
Echo ' <pre> ';
Var_dump ($row);
Echo ' </pre> ';
}
Close the database
Mysql_close ($con);
1 //Database Connection2 functionMysqlconnect () {3 $con=mysql_connect("localhost", "root", "" ");4 if(!$con){5 die("Connection Failed!".Mysql_error());6 Exit;7 }8 9Mysql_set_charset ("UTF8",$con);Ten mysql_select_db("MS",$con); One A /* - $sql = "SELECT * from User;"; - $result = mysql_query ($sql); the - if (mysql_num_rows ($result)) { - While ($rows = Mysql_fetch_assoc ($result)) { - echo ' <pre> '; + Var_dump ($rows); - echo ' </pre> '; + } A } at */ - - $sql= "INSERT into user (username) values (' test123 ')"; - $result=mysql_query($sql); - - if($result&&mysql_affected_rows() >0){ in Echo"Insert succeeded!".mysql_insert_id(); - } to + Mysql_close($con); -}
PHP MySQL Base operation