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