[Entry] PHP and database connection

Source: Internet
Author: User

Note: (use XAMPP)

1. Create a database:

Enter http: // localhost/PHPmyAdmin/in the address bar/

Follow the prompts on the page to create a database.

TIPS: You can select ID to automatically increase when creating a table.

2. Connect to the database:

 

Code

  1   <?  PHP
2 $ Dbhost = " Localhost " ;
3 $ Dbuser = " Root " ;
4 $ Dbpassword = " 1234 " ;
5
6 $ Dbdatabase = " Testaq " ;
7 $ DB = Mysql_connect ( $ Dbhost , $ Dbuser , $ Dbpassword );
8 Mysql_query ( " Set names 'utf8' " );
9 Mysql_select_db ( $ Dbdatabase , $ DB );
10   ?>

 

3. query the database:

 

 

  <?PHP
$ Searchcatagory = "Select * From catagory;";
$ Result = Mysql_query($ Searchcatagory);
?>

 

 

4. Insert new data:

 

  $ SQL _insert_qustion     =     "  Insert into questions (title, content, tags, time)
Values (' " . $ _ Post [ ' Title ' ] . " ',' " . $ _ Post [ ' Content ' ] . " ',' " . $ _ Post [ ' Tags ' ] . " ', Now ()); " ;
Mysql_query ( $ SQL _insert_qustion );

 

 

Obtain the ID number of the newly inserted entry:

 

  $ Questionid = Mysql_insert_id();

 

 

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.