Basic PHP operations on the MySQL database

Source: Internet
Author: User

When learning PHP, you have mastered the basic syntax. After learning about the variables, you should learn the basic operations php has on the mysql database.

<1> connect to the database using php.

The function used for mysql database connection is mysql_connect (servername, username, password). servername indicates the server to be connected. The default value is localhost. username refers to the database user name. My name is "root", and password refers to the password used for logon. The Code is as follows:

  1. <? Php$ Con= Mysql_connect ("Localhost","Root","111");If($ Con){Echo "Connection successful! ";}Else{Die('Could not connect :'. Mysql_error () ;}?>

<2> Create a php Database

You must connect to the database before creating the database, and then apply the mysql_query () function. Let's create a liyongquan_db database.

  1. <? Php
  2. $ Con= Mysql_connect ("Localhost","Root","111");
  3. If(!$ Con)
  4. {
  5. Die('Could not connect :'. Mysql_error ());
  6. }
  7. If(Mysql_query ("Create database liyongquan_db",$ Con))
  8. {
  9. Echo "The database is created successfully! ";
  10. }
  11. Else
  12. {
  13. Echo "Database creation failed :". Mysql_error ();
  14. }
  15. Mysql_close ($ Con);
  16. ?>

After running, view the result. Open navicat 8 for mysql and you can see:


<3> php inserts a table into the database

  1. <? Php
  2.  $ Con= Mysql_connect ("Localhost","Root","111");
  3.  If($ Con)
  4. {
  5. Echo "Connection successful! ";
  6. }
  7. Else
  8. {
  9. Die('Could not connect :'. Mysql_error ());
  10. }
  11. Mysql_select_db ("Liyongquan_db",$ Con);
  12. $ SQL= "Create table Persons
  13. (
  14. PersonID int not null AUTO_INCREMENT,
  15. Primary key (personID ),
  16. FirstName varchar (15 ),
  17. LastName varchar (15 ),
  18. Age int
  19. )";
  20. Mysql_query ($ SQL,$ Con);
  21. Mysql_close ($ Con);
  22. ?>

<4> insert data into a table

  1. <? Php
  2.  $ Con= Mysql_connect ("Localhost","Root","111");
  3.  If($ Con)
  4. {
  5. Echo "Connection successful! ";
  6. }
  7. Else
  8. {
  9. Die('Could not connect :'. Mysql_error ());
  10. }
  11. Mysql_select_db ("Liyongquan_db",$ Con);
  12. Mysql_query ("insert into Persons (FirstName, LastName, Age)
  13. VALUES ('Peter','Grigin','35')");
  14. Mysql_query ("insert into Persons (FirstName, LastName, Age)
  15. VALUES ('Glenn','Quagmire','33')");
  16. Mysql_close ($ Con);
  17. ?>

View results:

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.