Application analysis of php connection database code

  • 2020-05-05 11:04:22
  • OfStack

 
<?php 
$db_host='hostname is database server '; 
$db_database='database name'; 
$db_username='username'; 
$db_password='password'; 
$connection=mysql_connect($db_host,$db_username,$db_password);// Connect to the database  
mysql_query("set names 'utf8'");// Code conversion  
if(!$connection){ 
die("could not connect to the database:</br>".mysql_error());// Diagnostic connection error  
} 
$db_selecct=mysql_select_db($db_database);// Select database  
if(!$db_selecct) 
{ 
die("could not to the database</br>".mysql_error()); 
} 
$query="select * from msu ";// Build query statement  
$result=mysql_query($query);// Execute the query  
if(!$result) 
{ 
die("could not to the database</br>".mysql_error()); 
} 
// array mysql_fetch_row(resource $result); 
while($result_row=mysql_fetch_row(($result)))// Take the result and display it  
{ 
$num=$result_row[0]; 
$age=$result_row[1]; 
$name=$result_row[2]; 
echo "<tr>"; 
echo "<td>$num</td>"; 
echo "<td>$age</td>"; 
echo "<td>$name</td>"; 
echo "</tr>"; 
} 
mysql_close($connection);// Close the connection  
?> 

Below is the code
with detailed instructions

$connect = mysql_connect("127.0.0.1","root","") or die (" Link error ");// Open link to mysql 
$select_db = mysql_select_db(" The database name ",$connect);// If you do not specifically declare a connection identifier, you default to the last link that was opened  
// perform SQL Statement!  
$sql = "SELECT * FROM test" 
$query = mysql_query($sql,$connect) or die(mysql_error()); 

// Two kinds of query functions array/row The difference between  
$row1 = mysql_fetch_row($query); 
print_r($row1);// Only labels in an array can be saved  
$row2 = mysql_fetch_array($query); 
print_r($row2);// You can save the labels in the array, and the field names  
// Loop output while Output to an empty position  
while($row1){ 
print_r($row1); 
} 

mysql_query("SET NAMES 'UTF-8'"); 

// Used to calculate the number of query results  
mysql_num_rows($query); 
// Returns the last use INSERT The directive IP 
mysql_insert_id($query); 
// Get the database name  
mysql_tablename($query); 
// Error message returned  
mysql_error(); 
// Close links  
mysql_close(); 

PHP connects mySQL to
Mysql can be connected to web by PHP in two ways, one by Mysql correlation function of php and the other by ODBC correlation function of php.
The correlation function is as follows:
MYSQL function
mysql_affected_rows: gets the number of columns affected by MySQL's last operation.
mysql_close: close MySQL server connection.
mysql_connect: open MySQL server connection.
mysql_create_db: create a new MySQL database.
mysql_data_seek: move the internal return metrics.
mysql_db_query: send the query string (query) to MySQL database.
mysql_drop_db: removes the database.
mysql_errno: returns an error message code.
mysql_error: returns an error message.
mysql_fetch_array: returns array data.
mysql_fetch_field: gets the field information.
mysql_fetch_lengths: returns the maximum length of each column in a single column.
mysql_fetch_object: returns object data.
mysql_fetch_row: returns the fields of a single column.
mysql_field_name: returns the name of the specified field.
mysql_field_seek: sets the indicator to a field of the returned value.
mysql_field_table: gets the table (table) name for the current field.
mysql_field_type: gets the type of the current field.
mysql_field_flags: gets the flag for the current field.
mysql_field_len: gets the length of the current field.
mysql_free_result: releases the memory footprint by returning it.
mysql_insert_id: returns ID that last used the INSERT directive.
mysql_list_fields: lists the fields of the specified table (field).
mysql_list_dbs: lists the databases available on the MySQL server (database).
mysql_list_tables: lists the tables for the specified database (table).
mysql_num_fields: gets the number of fields returned.
mysql_num_rows: gets the number of returned columns.
mysql_pconnect: opens long term connection to MySQL server.
mysql_query: sends out an query string.
mysql_result: gets the result of the query (query).
mysql_select_db: select a database.
mysql_tablename: gets the table name.
ODBC function
To use the ODBC function, install MYSQL ODBC
odbc_autocommit: automatic switch change function.
odbc_binmode: sets the binary data processing mode.
odbc_close: close the ODBC link.
odbc_close_all: close all ODBC links.
odbc_commit: change the ODBC database.
odbc_connect: link to ODBC database.
odbc_cursor: gets the cursor name.
odbc_do: execute the SQL directive.
odbc_exec: execute the SQL directive.
odbc_execute: executes the preset SQL instruction.
odbc_fetch_into: gets the specified column returned.
odbc_fetch_row: gets a column returned.
odbc_field_name: gets the field name.
odbc_field_type: gets the field data form.
odbc_field_len: gets the length of the field data.
odbc_free_result: releases the memory of the returned data.
odbc_longreadlen: sets the maximum value of the return column.
odbc_num_fields: gets the number of fields.
odbc_pconnect: long-term link to ODBC database.
odbc_prepare: preset SQL instruction.
odbc_num_rows: gets the number of returned columns.
odbc_result: gets the returned data.
odbc_result_all: returns HTML form data.
odbc_rollback: cancel the current transaction.
odbc_setoption: adjust ODBC Settings.


Related articles: