Occasionally to use PHP to do some MySQL database operation Test, write yourself too troublesome, the results of the search generally contains a lot of useless code, here will be PHP MySQL operation to do a summary, I hope that in the future when the use of the time no longer feel trouble.
Copy Code code as follows:
<?php
$dbhost = ' localhost ';//database server name
$dbuser = ' root ';//Connection database user name
$dbpass = ' 123456 ';//Connect Database Password
$dbname = ' products ';//Name of the database
Connecting to a database
$connect =mysql_connect ($dbhost, $dbuser, $dbpass);
if (! $connect) exit (' Database connection failed! ');
mysql_select_db ($dbname, $connect);
mysql_query (' Set names UTF8 ');//Set encoding
Query operations
$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 succeeded, insert ID: ', mysql_insert_id ();
}else{
Echo ' Insert failed: ', mysql_error ();
}
Modify Operation
$sql = "UPDATE ' category ' SET ' name ' = '". $name. "' WHERE ' id ' = '. $id. "'";
$result =mysql_query ($sql);
if (Mysql_affected_rows ()) {
Echo ' modified successfully! ';
}
Delete operation
$sql = "DELETE from ' category ' WHERE ' id ' = '". $id. "'";
$result =mysql_query ($sql);
if (Mysql_affected_rows ()) {
Echo ' Delete success! ';
}
Close connection
Mysql_close ($connect);
?>