Common basic terms for relational databases
Data
Database
DBMS for database management systems
Table (data table) table
Fields field, column columns
Row row, recording record
Basic mode of database operations (process)
Establish a connection (authenticated identity)
Client sends SQL command to server side
The server side executes the command and returns the result of the execution
The client receives the result (and displays it)
Disconnect Connection
<?php
1. Connect to the database:
$mylink = mysql_connect ("localhost", ' root ', ');//Connect to Database
2, set the connection code (usually UTF8)
Mysql_set_charset ("UTF8"); can also be used: mysql_query ("Set names UTF8");
3. Select the database (if necessary)
mysql_select_db ("Temptab1"); Also available: mysql_query ("Use database name"); The database name must be
4, execute the SQL command.
$result = mysql_query ("Almost any SQL statement");
if (1 = = 1) {
$randName = rand (1000, 9999); Get a random number
$sql = "INSERT into TEMPTAB1 (ID, name) VALUES ($randName, ' user_$randname ');"; Basically write this SQL statement can, in the TEMPTAB1 table, first set up a id,name, to insert the data inside
$STR = "asf ' Afas ' Fafs"; The previous line of single quotes is just a normal character in PHP
$result = mysql_query ($sql); Executes the SQL statement, coexisting results
The results returned usually need to be handled in two different situations:
4.1: If it is a statement with no return data:
4.1.1 If $result is true, indicates successful execution
if ($result = = True) {
echo "Insert data Success";
}
4.1.2 If $result is false, execution fails
else{
echo "failed, please refer to the failure prompt message:". Mysql_error ();
}
}
The following begins the execution of the select "Return data" statement and displays the result
$sql = "SELECT * from TEMPTAB1 where ID > 1";
$result = mysql_query ($sql); If the execution succeeds, it is called a "dataset" (Result set)
if ($result = = = False) {
echo "failed, please refer to the failure prompt message:". Mysql_error ();
}
else{
echo "<table border= ' >";
while ($record = Mysql_fetch_array ($result)) {
The mysql_fetch_array () function acts like the function of a foreach array: traversal (Result set)
Each time it goes to the result set of "row of data" and "loads" into the array $record
The index of the array is the field name of the Select, and the value is the data value of the corresponding row
echo "<tr>";
echo "<td>". $record [' id ']. "</td>";
echo "<td>". $record [' name ']. "</td>";
echo "</tr>";
}
echo "</table>";
}
The following command to execute a non-select but return data displays the results
$sql = "show databases;"; The "DESC table name" command, which also gets the data
$result = mysql_query ($sql); If the execution succeeds, it is called a "dataset" (Result set)
if ($result = = = False) {
echo "failed, please refer to the failure prompt message:". Mysql_error ();
}
else{
echo "<table border= ' 1 ' >";
echo "<tr>";
echo "<td>Database</td>";
echo "</tr>";
while ($record = Mysql_fetch_array ($result)) {
The mysql_fetch_array () function acts like the function of a foreach array: traversal (Result set)
Each time it goes to the result set of "row of data" and "loads" into the array $record
The index of the array is the field name of the Select, and the value is the data value of the corresponding row
echo "<tr>";
echo "<td>". $record [' Database ']. "</td>";
echo "</tr>";
}
echo "</table>";
}
The following command to execute a non-select but return data displays the results
$sql = "show databases;";
$sql = "desc temptab1;";
$sql = "SELECT * from TEMPTAB1";
$result = mysql_query ($sql); If the execution succeeds, it is called a "dataset" (Result set)
if ($result = = = False) {
echo "failed, please refer to the failure prompt message:". Mysql_error ();
}
else{
$fieldCount = Mysql_num_fields ($result); Get the number of fields in the result set
echo "<table border= ' 1 ' >";
Output header section, Content is field name:
echo "<tr>";
for ($i = 0; $i < $fieldCount; + + $i) {//"traverse" all columns in each row)
$fieldName = Mysql_field_name ($result, $i);//The name of the first field taken from the result set (I count from 0)
echo "<td>". $fieldName. "</td>";
}
echo "</tr>";
while ($record = Mysql_fetch_array ($result)) {
The mysql_fetch_array () function acts like the function of a foreach array: traversal (Result set)
Each time it goes to the result set of "row of data" and "loads" into the array $record
The index of the array is the field name of the Select, and the value is the data value of the corresponding row
echo "<tr>";
echo "<td>". $record [' Field ']. "</td>";
echo "<td>". $record [' Type ']. "</td>";
echo "</tr>";
The above code will write several items, but what if the field name (and number of fields) is not determined?
echo "<tr>";
for ($i = 0; $i < $fieldCount; + + $i) {//"traverse" all columns in each row)
$fieldName = Mysql_field_name ($result, $i);//The name of the first field taken from the result set (I count from 0)
echo "<td>". $record [$fieldName]. "</td>";
}
echo "</tr>";
}
echo "</table>";
}
?>
Transferred from: http://www.cnblogs.com/shiyou00/p/5576667.html
Database Basics (1)--Database PHP connection