Simple MySQL database operations

Source: Internet
Author: User
Tags mysql host

<? 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


Related Article

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.