<? PHP
// Create a database link,
Mysql_connect ("localhost", "mysql_user", "mysql_password") or die ("cocould not connect:". mysql_error ());
// Select a database
Mysql_select_db ("mydb ");
// Set the character set
Mysql_query ('set names utf8') or die ('character set setting error ');
// Query SQL statements
$ Result = mysql_query ("select ID, name from mytable ");
// Output query results
While ($ ROW = mysql_fetch_array ($ result )){
Echo $ row ['id'], "<br/>", $ row ['name'];
}
// Release the result memory
Mysql_free_result ($ result );
// Close the database
Mysql_close ();
?>
Create an instance for a table:
Create Table 'bbs '. 'guest _ Book '(
'Id' tinyint (6) Not null auto_increment primary key,
'Edu' varchar (20) Character Set gb2312 collate gb2312_chinese_ci not null,
'Age' varchar (20) Character Set gb2312 collate gb2312_chinese_ci not null,
'Phone' varchar (20) Character Set gb2312 collate gb2312_chinese_ci not null,
'Xiangmu 'varchar (20) Character Set gb2312 collate gb2312_bin not null,
'Time' datetime not null
) Engine = MyISAM
Add or delete the query
Add data
<? PHP
$ Query = "insertintograde (name, email, point, regdate) value ('Li Yanhui ', 'y16a. com@gmail.com', now ())";
@ Mysql_query ($ query) or die ('data addition error: '. mysql_error ());
?>
Modify data
<? PHP
$ Query = "updategrade set name = 'cute kid' whereid = 6"; @ mysql_query ($ query) or die ('modification error: '. mysql_error ());
?>
Delete data
<? PHP
$ Query = "delete from grade whereid = 6"; @ mysql_query ($ query) or die ('deletion error: '. mysql_error ());
?>
Show data
<? PHP
$ Query = "select ID, name, email, point from Grade ";
$ Result = @ mysql_query ($ query) or die ('query statement error: '. mysql_error (); While (!! $ ROW = mysql_fetch_array ($ result )){
Echo $ row ['id']. '----'. $ row ['name']. '----'. $ row ['email ']. '----'. $ row ['point']; echo '<br/> ';
}
?>
Other common functions
Mysql_fetch_row (): Get a row from the result set as an enumeration array mysql_fetch_assoc (): Get a row from the result set as an associated array
Mysql_fetch_array (): gets a row from the result set as an associated array, or an array of numbers, or both.
Mysql_fetch_lengths (): gets the length of each output in the result set. mysql_field_name (): gets the field name of the specified field in the result.
Mysql_num_rows (): number of rows in the result set. mysql_num_fields (): number of fields in the result set.
Mysql_get_client_info (): Obtain MYSQL client information mysql_get_host_info (): Obtain MySQL host information
Mysql_get_proto_info (): Obtain MySQL protocol information mysql_get_server_info (): Obtain MySQL Server Information