MySQL connection and basic operation code in PHP (Quick test use, simple and convenient) _php tutorial

Source: Internet
Author: User
Tags php mysql
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 ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.