Occasionally to use PHP to do some of the MySQL database operation Test, their own writing is too cumbersome, the results of the search generally contain a lot of useless code, here will PHP MySQL operation to summarize, I hope that later use when no longer feel trouble.
Copy the Code code as follows:
$dbhost = ' localhost ';//database server name
$dbuser = ' root ';//Connect database user name
$dbpass = ' 123456 ';//Connect Database Password
$dbname = ' products ';//Database name
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 succeeded! ';
}
Close connection
Mysql_close ($connect);
?>
http://www.bkjia.com/PHPjc/761019.html www.bkjia.com true http://www.bkjia.com/PHPjc/761019.html techarticle occasionally to use PHP to do some of the MySQL database operation Test, their own writing is too cumbersome, the results of search generally contain a lot of useless code, here will php MySQL operation to do ...