Occasionally, you need to use php to perform some mysql Database Operation tests. It is too troublesome to write by yourself. The search results generally contain a lot of useless code. Here we will summarize the operations of php mysql, I hope you don't have to worry about it in the future.
Copy codeThe Code is as follows:
<? Php
$ Dbhost = 'localhost'; // database server name
$ Dbuser = 'root'; // user name used to connect to the database
$ Dbpass = '000000'; // password used to connect to the database
$ Dbname = 'products'; // Database Name
// Connect to the database
$ Connect = mysql_connect ($ dbhost, $ dbuser, $ dbpass );
If (! $ Connect) exit ('database connection failed! ');
Mysql_select_db ($ dbname, $ connect );
Mysql_query ('set names utf8'); // sets the Encoding
// Query operation
$ SQL = "SELECT * FROM 'category '";
$ Result = mysql_query ($ SQL );
While ($ row = mysql_fetch_array ($ result )){
Echo $ row ['id'];
}
// Insert operation
$ SQL = "INSERT INTO 'category '('id', 'name') VALUES (NULL,'". $ name ."')";
$ Result = mysql_query ($ SQL );
If (mysql_affected_rows ()){
Echo 'insert successful, insert ID: ', mysql_insert_id ();
} Else {
Echo 'insertion failed: ', mysql_error ();
}
// Modify the operation
$ SQL = "UPDATE 'category' SET 'name' = '". $ name. "'where' id' ='". $ id ."'";
$ Result = mysql_query ($ SQL );
If (mysql_affected_rows ()){
Echo 'modification successful! ';
}
// Delete operation
$ SQL = "DELETE FROM 'category' WHERE 'id' = '". $ id ."'";
$ Result = mysql_query ($ SQL );
If (mysql_affected_rows ()){
Echo 'deleted! ';
}
// Close the connection
Mysql_close ($ connect );
?>