This article illustrates the way PHP operates MySQL. Share to everyone for your reference, specific as follows:
Set all UTF-8 encoding for Zend Software
Set the encoding for a single project UTF-8
demo1.php
<?php header (' content-type:text/html; charset=utf-8; '); First step, connect to Mysql server 3306//second step parameter, server address; second parameter, server username; third parameter, server password//@ if error, do not appear warning or error, directly ignore//die function, first connect, error process Echo (!!
mysql_connect (' localhost ', ' root ', ' 123456 ')); if (! $conn = @mysql_connect (' localhost ', ' root ', ' 123456 ')) {//Echo ' Database connection failed, error message '. Mysql_error ();//exit;//}//EC
Ho $conn; Echo ' connected successfully, I can show!
';
Constant parameter define (' Db_host ', ' localhost ');
Define (' Db_user ', ' root ');
Define (' Db_pwd ', ' 123456 ');
Define (' db_name ', ' school '); The first step is to connect the database//mysql_connect--open a connection to the MySQL server $conn = @mysql_connect (db_host,db_user,db_pwd) or Die (' Database connection failed, error message '
. Mysql_error ());
The second step, select the specified database, set the character set//mysql_select_db--Select MySQL database mysql_select_db (db_name) or Die (' Database error, error message: '. mysql_error ());
mysql_query (' Set NAMES UTF8 ') or Die (' character set setting error, error message '. Mysql_error ());
The third step, select a table from this database (grade), and then put the database of this table (get Recordset) $query = "SELECT * from grade"; Mysql_query--Send a MySQL query $result = mysql_query ($query) or Die (' SQL error: '. mysql_error ()); $result is the recordset//Fourth step, showing the data in the recordset Print_r (Mysql_fetch_array ($result, Mysql_num));//digitally subscript to display//print_r (mysql_fetch _array ($result, MYSQL_ASSOC));
Displays Print_r (Mysql_fetch_array ($result, Mysql_num) according to the string subscript;
Print_r (Mysql_fetch_array ($result, mysql_num));
Step fifth, release the Recordset resource//mysql_free_result--Release The result memory Mysql_free_result ($result);
Last step: Close the database//mysql_close--close MySQL connection echo mysql_close ();?>
config.php
<?php
header (' content-type:text/html; charset=utf-8; ');
Constant parameter
define (' db_host ', ' localhost ');
Define (' Db_user ', ' root ');
Define (' Db_pwd ', ' 123456 ');
Define (' db_name ', ' School ');
The first step is to connect the MySQL server
$conn = @mysql_connect (db_host,db_user,db_pwd) or Die (' Database connection failure, error message '. Mysql_error ());
In the second step, select the specified database, set the character set
mysql_select_db (db_name) or Die (' Database error, error message: '. mysql_error ());
mysql_query (' Set NAMES UTF8 ') or Die (' character set setting error, error message '. Mysql_error ());
? >
demo2.php
<?php require ' config.php '; New data//$query = "INSERT into grade (//name,//email,//point,//RegDate)//VALUES (//' landscape ',//' Jly
@163.com ',//', '//Now ()//) ';
$query = "INSERT into grade (name,email,point,regdate) VALUES (' Shared ', ' abc@163.com ', ', '", Now ()) ";
mysql_query ($query) or Die (' New error: '. mysql_error ());
Modify Data//$query = ' UPDATE grade SET point=66 WHERE id = 7 ';
@mysql_query ($query) or Die (' Modify failed: '. mysql_error ());
Delete data//$query = ' Delete from grade WHERE id= 4 ';
@mysql_query ($query) or Die (' Delete failed: '. mysql_error ());
Display data $query = ' SELECT id,name,email from grade ';
$result = mysql_query ($query) or Die (' SQL statement error: '. mysql_error ());
$row = Mysql_fetch_array ($result);
Echo $row [2];
$row = Mysql_fetch_array ($result);
Echo $row [2]; The result set is converted to $row, if there is data, it is true while!! $row = mysql_fetch_array ($result)) {echo $row [' id ']. ' ----'. $row [' name ']. '
-----'. $row [' email '];
Echo ' <br/> '; } mysql_close ();
?>
Demo3.php
<?php require ' config.php ';
Display data $query = ' SELECT id,email,name from grade ';
$result = mysql_query ($query) or Die (' SQL statement error: '. mysql_error ());
Print_r (Mysql_fetch_array ($result));
Print_r (Mysql_fetch_array ($result, MYSQL_ASSOC));
Print_r (Mysql_fetch_row ($result));
Print_r (MYSQL_FETCH_ASSOC ($result)); while (!! $row = mysql_fetch_array ($result)) {//Echo $row [' id ']. ' ----'. $row [' name ']. '
-----'. $row [' email '];
Print_r (Mysql_fetch_lengths ($result));
Echo Mb_strlen ($row [' name '], ' utf-8 ');
Echo ' <br/> '; //echo Mysql_field_name ($result, 2); Name//echo mysql_num_fields ($result);
3 for ($i =0 $i <mysql_num_fields ($result); $i + +) {echo mysql_field_name ($result, $i);//id----Email----name----
echo '----';
Echo ' <br/> '; echo mysql_num_rows ($result);
Find out how many data echo ' <br/> ';
echo mysql_get_client_info ()//Get MySQL client information//5.0.51a echo ' <br/> '; echo mysql_get_host_info ()//Get MySQL host information//localhoSt via TCP/IP echo ' <br/> ';
echo mysql_get_proto_info ()//Get MySQL protocol information//10 Echo ' <br/> ';
echo mysql_get_server_info ()//Get MySQL server information//5.0.51b-community-nt-log mysql_close ();?>
For more information about PHP interested readers can view the site topics: "Php+mysql database operations to get Started", "PHP basic Grammar Introductory Course", "PHP Operations and Operator Usage Summary", "PHP object-oriented Program Design Introductory Course", "PHP Network Programming Skills Summary", " PHP array Operation tips Encyclopedia, PHP string (String) Usage summary and PHP Common database operation Skills Summary
I hope this article will help you with the PHP program design.