PHP using MySQL extension to execute a SELECT query examples

To determine whether to open a MySQL extension

To use the MySQL extension, first determine whether to open a MySQL extension.

. 1 ? < PHP 
 2  header ( "the Content-the Type: text / HTML; the CHARSET = UTF8" );
 . 3  
. 4  // function_exists - if the given function has been defined returns TRUE
 . 5  // HTTPS: //www.php. NET / Manual / ZH / function.function-exists.php 
. 6  IF (! function_exists ( "mysql_connect" )) {
 . 7      Exit ( 'the MySQL extension is not turned on' );
 8 }

 

Reads the configuration file

Read MySQL connection information required, host, username, password, etc.

1 // parse_ini_file — 解析一个配置文件
2 // https://www.php.net/manual/zh/function.parse-ini-file.php
3 $mysqlIni = parse_ini_file("mysql.ini");
4 $host = $mysqlIni['host'];
5 $port = $mysqlIni['port'];
6 $username = $mysqlIni['username'];
7 $password = $mysqlIni['password'];
8 $dbname = $mysqlIni['dbname'];

 

MySQL connection

Use MySQL connection mysql_connect

. 1  // mysql_connect - Open a connection to a MySQL server
 2  // https://www.php.net/manual/zh/function.mysql-connect.php 
. 3  $ Link = @ mysql_connect ( "{ $ Host }: { Port $ } ", $ username , $ password );
 . 4  
. 5  // decided whether or not the connection is successful 
. 6  IF (! $ Link ) {
 . 7      echo 'database connection failure <br>' ;
 . 8      // mysql_errno - a return operation MySQL the digitally encoded error message
 . 9      // https://www.php.net/manual/zh/function.mysql-errno.php 
10      echo 'error number:',mysql_errno (), '<br>' ;
 . 11      // mysql_error - Returns the MySQL operation information generated by the error
 12 is      // https://www.php.net/manual/zh/function.mysql-error.php 
13 is      echo 'error message:', mysql_error ();
 14      Exit ();
 15 }

 

Select the database (database)

Select the database where the tables to be operated

. 1  // the mysql_select_db - Select MySQL database
 2  // https://www.php.net/manual/zh/function.mysql-select-db.php 
. 3  $ db_selected = the mysql_select_db ( $ dbname , $ Link );
 . 4  IF ( ! $ db_selected ) {
 . 5      echo 'selected database fails <br>' ;
 . 6      echo 'error number:', mysql_errno (), '<br>' ;
 . 7      echo 'error message:', mysql_error ();
 . 8      Exit () ;
 9 }

 

Character Set

Set character set is utf8, avoid Chinese garbled

1 mysql_query("set names utf8");

 

Send MySQL Query

Send MySQL query, if the query is successful, after processing the result set to release the result set. Here first release on the result set to write, write in the result set back

1  // define the query 
2  $ Query = "the SELECT the above mentioned id, name, height, Gender, date_of_birth from starinfo limit 0,5" ;
 3  
4  // mysql_query - Send a MySQL query
 5  // HTTPS: //www.php. NET / Manual / ZH / function.mysql-query.php 
. 6  $ Result = the mysql_query ( $ query , $ Link );
 . 7  IF (! $ Result ) {
 . 8      echo 'query failed <br>' ;
 . 9      echo 'error number: ', mysql_errno ();
 10      echo ' error message: ', mysql_error ();
. 11      Exit ();
 12 is  }
 13 is  
14      // here the result set process
 15      // here the result set process
 16      // here the result set process
 . 17  
18 is  // mysql_free_result - release the result memory
 19  @ https://www.php.net/manual/zh/function.mysql-free-result.php 
20 is  mysql_free_result ( $ Result );
 21 is  
22 is  // mysql_close - Close MySQL connection
 23 is  // HTTPS: //www.php. NET / Manual / ZH / function.mysql-close.php 
24  mysql_close ( $ Link );

 

Result set

Here was added to remove the associative array foreach loop output a while loop form Example

. 1  echo "<Table border = \" 1px \ ">" ;
 2  
. 3  // take the field name 
. 4  echo "<TR>" ;
 . 5  // The mysql_fetch_field - column information acquired from a result and return as an object
 . 6  // HTTPS: //www.php.net/manual/zh/function.mysql-fetch-field.php 
. 7  the while ( $ Field = The mysql_fetch_field ( $ Result )) {
 . 8      echo "<TH> { $ Field -> name} </ TH > " ;
 . 9  }
 10  echo " </ TR> " ;
. 11  
12 is  // get record information
 13 // mysql_fetch_array - Fetch a result row as an associative array, or an array of numbers, or both
 14  // https://www.php.net/manual/zh/function.mysql-fetch-array.php 
15  the while ( $ Row = the mysql_fetch_array ( $ Result , MYSQL_ASSOC)) {
 16      echo "<TR>" ;
 . 17      the foreach ( $ Row  AS  $ Key => $ value ) {
 18 is          echo "<TD> { $ value } </ TD>" ;
 . 19      }
 20 is      echo "</ TR>" ;
 21 is  }
 22 is  
23 is  echo "</table>";

 

Final output

id name height gender date_of_birth
1 Hu Ge 185 male 1982-09-20
2 Yang Mi 167 female 1986-09-12
3 Zhao Wei 166 female 1976-03-12
4 Zhao Liying 165 female 1987-10-16
5 Alec 173 male 1973-09-11

Guess you like

Origin www.cnblogs.com/baimi/p/11862759.html