PHP Operations Database

Source: Internet
Author: User

A PHP connection to MySQL

////Comparison specification is the address, login name, password so write, more secure  Define("Db_host", ' localhost '); Define(' Db_user ', ' root '); Define("Db_pwd", ' kang123456 '); Define(' db_name ', ' school '); //1 connecting the database  $conn= @mysql_connect(DB_HOST,DB_USER,DB_PWD) or die(' Database connection failed '.Mysql_error()); //2 Select the specified database, the second parameter resource handle can not be put; Select Character Set  mysql_select_db(Db_name,$conn) or die(' Database not found, error: '.Mysql_error()); mysql_query(' SET NAMES UTF8 ') or die(' Character set error '.Mysql_error());//Note that this is not utf-8//3 get the recordset: Select a table from the database and put the table data  $query= "Select *from Grade"; $result= @mysql_query($query) or die(' SQL error: '.Mysql_error());//returns the resource handle type, result is the Recordset, can be added with an error suppression symbol @//4 to display the information in the recordset, result is the recordset  Print_r(Mysql_fetch_array($result));//returns an array, the second parameter can be returned by a numeric subscript and a string subscript//5 free Recordset Resource  Mysql_free_result($result); 6Close the databaseMysql_close();//Close successful return 1

Two Change and delete

  //1 New Data  $query= "INSERT into Grade (Id,name,email,point,regdate) VALUES (5, ' Kobayashi ', ' [email protected] ', 45,now ())"; @mysql_query($query) or die(' Query error: '.Mysql_error()); //2 Modifying Data  $query= "Update grade set point=222 where id=1"; @mysql_query($query) or die(' Query error '.Mysql_error()); //3 Deleting Data  $query= "Delete from grade where id=5"; mysql_query($query) or die(' Query error: '.Mysql_error()); //4 Querying Data  $query= "Select *from Grade"; $result= @mysql_query($query) or die("Query Error:".)Mysql_error()); //5 Displaying Data  $query= "Select *from Grade"; $result= @mysql_query($query) or die("Query Error:".)Mysql_error());  while(!!$row=Mysql_fetch_array($result)){//mysql_fetch_array (); will automatically move down after assigning one value      Echo $row[' name ']. ' --‘.$row[' Point '];//you can also [number]      Echo' <br/> '; } 

Three Other common functions

Mysql_fetch_row (): gets a row from the result set as an enumeration array

MYSQL_FETCH_ASSOC (): gets a row from the result set as an associative array

Mysql_fetch_array (): gets a row from the result set as an associative array, or as a numeric array, 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 (): Gets the number of rows in the result set

Mysql_num_fields (): Gets the number of fields in the result set

Mysql_get_client_info (): get MySQL Client Information

Mysql_get_host_info (): get MySQL host information

Mysql_get_proto_info (): get MySQL protocol information

Mysql_get_server_info (): get MySQL Server information

    //mysql_fetch_lengths (): Gets the length of each output in the result set    $query= "Select *from Grade"; $result= @mysql_query($query) or die("Query Error:".)Mysql_error());  while(!!$row=Mysql_fetch_array($result)){//mysql_fetch_array (); will automatically move down after assigning one value        Echo $row[' name ']. ' --‘.$row[' Point '];//you can also [number]        Print_r(mysql_fetch_lengths($result));//shows the number of bytes per field, so how do you get the number of words in Chinese?        EchoMb_strlen ($row[' name ']);//get the number of Chinese characters in name        Echo' <br/> '; }        //mysql_field_name (): Gets the field name of the specified field in the result    Echo Mysql_field_name($result, 3);//fourth field name//mysql_num_rows (): Gets the number of rows in the result set    Echo mysql_num_rows($result);//5 rows of Data//mysql_num_fields (): Gets the number of fields in the result set    Echo Mysql_num_fields($result);//5 column Fields

PHP Operations Database

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.