PHP getting started tutorial-Analysis of PHP operations on MySQL, Getting Started tutorial mysql

Source: Internet
Author: User
Tags mysql host sql error utf 8

PHP getting started tutorial-Analysis of PHP operations on MySQL, Getting Started tutorial mysql

This example describes how to operate MySQL in PHP. We will share this with you for your reference. The details are as follows:

Set all UTF-8 codes for Zend Software

Sets the encoding of a single project UTF-8

Demo1.php

<? Php header ('content-Type: text/html; charset = UTF-8; '); // The first step is to connect to Mysql Server 3306 // The second step parameter and the server address; the second parameter is the server user name. The third parameter is the server password // @. If an error occurs, do not generate a warning or error. Before directly ignoring the // die function, connect to the server first, error Reporting Process // echo (!! Mysql_connect ('localhost', 'root', '123'); // if (! $ Conn = @ mysql_connect ('localhost', 'root', '000000') {// echo 'database connection failed, error information '. mysql_error (); // exit; //} // echo $ conn; // echo: the connection is successful! '; // Constant parameter define ('db _ host', 'localhost'); define ('db _ user', 'root'); define ('db _ pwd ', '20140901'); define ('db _ name', 'school'); // step 1, connect to 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 information '. mysql_error (); // step 2, select the specified database and 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 UTF 8 ') or die ('character set setting error, error information '. mysql_error (); // Step 3: select a table (grade) from the database, and then propose the database of the table (obtain the record set) $ query = "SELECT * FROM grade"; // mysql_query -- send a MySQL query $ result = mysql_query ($ query) or die ('SQL error :'. mysql_error (); // $ result is the fourth step of the record set // display the data in the record set print_r (mysql_fetch_array ($ result, MYSQL_NUM )); // display by Numeric subscript // print_r (mysql_fetch_array ($ result, MYSQL_ASSOC); // display print_r (mysql_fetch_array ($ resu Lt, MYSQL_NUM); print_r (mysql_fetch_array ($ result, MYSQL_NUM); // step 5, release the record set resource // mysql_free_result -- release the result memory mysql_free_result ($ result ); // last step: Close the database // mysql_close -- close the 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', '000000'); define ('db _ name', 'school '); // Step 1: connect to the MYSQL server $ conn = @ mysql_connect (DB_HOST, DB_USER, DB_PWD) or die ('database connection failed, error information '. mysql_error (); // step 2, select the specified database and 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 Error, error message '. mysql_error ();?>

Demo2.php

<? Php require 'config. php'; // Add data // $ query = "insert into grade (// name, // email, // point, // regdate) // VALUES (// 'jing Jing ', // 'jly @ 163.com', // '78 ', // NOW ()//)"; // $ query = "insert into grade (name, email, point, regdate) VALUES ('col', 'abc @ 163.com ', '78', NOW ())"; // mysql_query ($ query) or die ('add error :'. mysql_error (); // modify data // $ query = 'Update grade SET point = 66 WHERE id = 7'; // @ mysql_query ($ query) or di E ('modification failed :'. mysql_error (); // DELETE data // $ query = 'delete FROM grade WHERE id = 4'; // @ mysql_query ($ query) or die ('deletion failed: '. mysql_error (); // display data $ query = 'select id, name, email FROM grad'; $ 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]; // convert the result set to an array and assign it 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 grad'; $ result = mysql_query ($ query) or die (' SQL statement error :'. mysql_error (); // print_r (mysql_fetch_array ($ result, MYSQL_ASSOC); // print_r (cost ($ 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); // how many pieces of data are obtained echo '<br/>'; echo mysql_get_client_info (); // obtain MySQL client information // 5.0.51a echo '<br/>'; echo mysql_get_host_info (); // obtain MySQL host information // localhost via TCP/IP echo '<br/>'; echo mysql_get_proto_info (); // obtain MySQL protocol information // 10 echo '<br/>'; echo mysql_get_server_info (); // obtain MySQL Server Information // 5.0.51b-community-nt-log mysql_close ();?>

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.