MySQL connection and basic operation code in PHP

Source: Internet
Author: User
Tags php mysql mysql database

  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.

  Code as follows: <?php $dbhost = ' localhost ';//database server name $dbuser = ' root ';//connection database username $dbpass = ' 123456 ';//Connect database Password $dbname = ' Products '//database name  //connection to 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 Operation $sql = "SELECT * from ' Category '"; $result =mysql_query ($sql); while ($row =mysql_fetch_array ($result)) { echo $row [' id '];}//Insert action $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);?>   &NBSp
Related Article

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.