MYSQL entry 2 operation MYSQL_MySQL

Source: Internet
Author: User
MYSQL getting started _ PHP for MYSQL bitsCN.com

Getting started with MYSQL _ PHP

1. connect to MySQL locally

$ Con = mysql_connect ("localhost", "root", "xxxxxx ");

If (! $ Con ){

Die ('could not connect: '. mysql_error ());

} Else {

Echo "Connect success
/N ";

}

2. SAEs connect to MySQL

$ Con = mysql_connect (SAE_MYSQL_HOST_M. ':'. SAE_MYSQL_PORT,

SAE_MYSQL_USER, SAE_MYSQL_PASS );

If (! $ Con ){

Die ('could not connect: '. mysql_error ());

} Else {

Echo "Connect success
/N ";

}

Mysql_select_db (SAE_MYSQL_DB, $ con );

3. Disconnect MySQL

Mysql_close ($ con );

4. create a database

$ SQL = "CREATE DATABASE IFNOT EXISTS $ DATABASE_NAME ";

If (mysql_query ($ SQL, $ con )){

Echo "Database create success
/N ";

} Else {

Echo "Error creating database:". mysql_error ()."
/N ";

}

5. connect to the database

Mysql_select_db ($ DATABASE_NAME, $ con );

6. create a data table

$ SQL = "CREATE TABLE $ TABLE_NAME (

Namevarchar (80 ),

Ageint

) ENGINE = MyISAM default charset = utf8 ";

If (mysql_query ($ SQL, $ con )){

Echo "Table create success
/N ";

} Else {

Echo "Error creating table:". mysql_error ()."
/N ";

}

7. search for data

$ SQL = "SELECT * FROM $ TABLE_NAME where name like/" % $ INPUT % /"";

$ Result = mysql_query ($ SQL, $ con );

While ($ row = mysql_fetch_array ($ result )){

Echo $ row ['name']. "". $ row ['age'];

Echo"
";

}

8. set the character set (the configured SQL operation is based on the utf8 character set)

Mysql_query ("set names 'utf8 '");

Mysql_query ("SET CHARACTER_SET_CLIENT = utf8 ");

Mysql_query ("SET CHARACTER_SET_RESULTS = utf8 ");

BitsCN.com

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.