I said PHP can not mention MySQL, but to talk about MySQL, then PHP is bound to be mentioned. The rapid rise of PHP can not be separated from MySQL, and MySQL is widely used, but also closely related to PHP.
The following is a detailed analysis of PHP4 in the MySQL-related operations function (a total of 32, beginning with Mysql_):
<1>. Functions to connect to the database server (2):
The port parameter in the parameter represents the port number of the database server, usually with its default port number.
If you do not fill in any parameters, the default hostname is localhost,username to root,password null.
function succeeded, returned an int type connection number (Link_identifier), failed, and returned a value of false.
Example:
$connect = mysql_connect ("localhost", "User", "password");
if ($connect) echo "Connect successed!"; Connection successful, show connect successed!
else echo "Connect failed!"; Connection failed, show connect failed!
?>
In the example above, if mysql_connect () fails, the system's error prompts are displayed, and then the execution continues. So, how do you block these system error prompts and end the program after the failure?
In MySQL, allow the database function to be preceded by the @ symbol, shielding the system's error prompts, with the Die () function to give more understandable error prompts, and then the Die () function will automatically exit the program.
The previous example could read:
$connect = @mysql_connect ("localhost", "User", "password") or Die ("Unable to connect database server!");
?>
If the mysql_connect () execution fails, the Unable to connect database server! will be displayed after exiting the program.
(2). Mysql_pconnect ()
Format: int mysql_pconnect (string [hostname] [ort],string [username],string [password]);
This function is essentially the same as the mysql_connect () of (1), except that:
---------when the database operation is complete, the connection established by (1) mysql_connect () is automatically closed, and (2) the connection established by Mysql_pconnect () will continue to exist, a solid and persistent connection.
---------in (2) mysql_pconnect (), before each connection, will check whether there is a connection using the same hostname,use,password, if so, use this connection number directly.
The connection established by the mysql_connect () of the---------(1) can be closed with mysql_close (), and (2) mysql_pconnect () cannot be closed with mysql_close ().
<2> Close database connection functions (1):
Mysql_close ()
Format: int mysql_close (int link_identifier);
Closes the connection established by the mysql_connect () function, performs successfully, returns a Ture value, and returns a value of false for failure.
Examples are as follows:
$connect = @mysql_connect ("hostname", "User", "password") or Die ("Unable to connect database server!");
$close = @mysql_close ($connect) or Die ("Unable to close database server connect!");
?>
Note: mysql_close () cannot close the connection established by the Mysql_pconnect () function.
<3> Select a database function (1):
mysql_select_db ()
Format: int mysql_select_db (string database name, int link_identifier);
Selects the specified database name, succeeds, returns 1 Truth (True) and fails, returns 1 false values
Example 1:
$select = mysql_select_db (\ ' forum\ ', $connect);
if ($select)
{echo "Connect db forum successed!";}
Else
{echo "Connect db forum failed!";}
?>
Example 2:
$select = mysql_select_db ("forum", $connect) or Die ("Can not connect this db!");
?>
Note: This function is equivalent to the USE statement in MySQL: Use forum
<4>. SQL query Functions (2):
1, mysql_query ()
Format: int mysql_query (string sqlquery, int link_identifier);
Send a standard SQL statement request to the server. If it fails, a false value is returned.
Example:
$connect = mysql_connect ($hostname, $user, $pwd);
$select = mysql_select_db ($dbname, $connect);
$query = mysql_query ($sql, $connect);
if ($query) echo "Successed!";
else echo "Failed!";
?>
This function must be used in conjunction with the mysql_select_db () function, it is meaningless to use it alone!
2, Mysql_db_query ()
Format: int mysql_db_query (String database, string sqlquery, int link_identifier);
Database names and SQL statement sqlquery must be specified in this function, and False if failed.
The difference between mysql_db_query () and mysql_query () is that the former can select database databases without using mysql_select_db (), while the SQL statement is executed while the database is selected. <5>. Database record operation function (5):
Successful execution returns an array that holds the value of the next record, and returns a value of False if execution fails.
The returned array can be represented either by subscript or by field name.
The function of the mysql_fetch_array () function is basically the same as 1. The difference is that mysql_fetch_row () can only be represented as an array subscript.
A successful return of an array failed to return a value of false.
Example:
$query = mysql_query ($sql, $connect);
while ($row = Mysql_fetch_row ($query))
{
echo $row [0]. " | " . $row [1]. "
";
}
?>
The Note:mysql_fetch_row () function can only be represented by an array subscript, starting at 0.
Another: Mysql_fetch_row () performs faster than Mysql_fetch_array () and reads the next row of data.
3, Mysql_result ()
Format: int mysql_result (int query, int row, string filedname);
In Mysql_result (), the argument row must start at 0, and the parameter filedname must be a real field name and cannot be represented by a subscript.
Successful execution returns the value of the field retrieved from the database, and returns a false values if it fails.
The Note:mysql_fetch_object () function returns 1 objects after successful execution.
The operation is as follows:
$object-> Field Name
5, Mysql_data_seek ()
Format: int mysql_data_seek (int row, int query);
Move the cursor to the specified row (row_number)
Execution succeeded, returned a true value, failed, and returned false.
This function can be used in conjunction with mysql_fetch_array () or mysql_fetch_row (), which means that after using the Mysql_data_seek () function, you can use the mysql_fetch_array () or Mysql_fetch_ Row () function to display the specified line.
Note: If you use mysql_query () or Mysql_db_query (), the SQL statement should be:
(1) Create DATABASE dbtest
(2) Drop DB dbtest 7) database information function (2):
1, Mysql_fetch_field ()
Format: Object Mysql_fetch_field (int query, int [field_offset]);
Returns 1 objects, a hash table, labeled:
Table: Tables Name
Name: Field name
Max_length: Maximum length of this field
Not_null: Field NOT NULL returns 1, otherwise returns 0
Primary_key: Field Primary key returns 1, otherwise returns 0
Unique_key: Field Unique key returns 1, otherwise returns 0
Multiple_key: field is not unique key returns 1, otherwise returns 0
Numeric: Field numeric returns 1, otherwise returns 0
Blob: Field BLOB returns 1, otherwise returns 0
Type: Types of fields
Unsigned: Field unsigned returns 1, otherwise returns 0
Zerofill: Field Zero filled returns 1, otherwise returns 0
The reference format is: Object name-> subscript name
Use this function to get the table name, field name, type ....
Note: The hash table starts with 0 coordinates, that is, the first field is the 0 items in the Hashtable.
If we want to get directly to the third or third field of the hash table, you can use the following format:
$query = mysql_query ($sql, $connect);
$object = Mysql_fetch_field ($query, 2);
echo "Table name:". $object->table. "
";
echo "Field name:". $object->name. "
";
echo "PRIMARY key:". $object->primary_key. "
";
echo "NOT null:". $object->not_null. "
";
echo "field type:". $object->type. "
";
echo "Field max Length:". $object->max_length. "
";
?>
In fact, this can also be done by the following function to achieve the same goal.
2, Mysql_field_seek ()
Format: int mysql_field_seek (int $query, int field_offset);
Moves the cursor over the specified field.
Example:
This also meets the same requirements as the previous example.
8) Take the database name and table name (2):
1, Mysql_list_dbs ()
Format: int Mysql_list_dbs (int link_identifier);
Gets all available database names (db name).
Example:
$connect = mysql_connect ($host, $USR, $pwd);
$dbs = Mysql_list_dbs ($connect);
$rows = mysql_num_rows ($dbs);
echo "Database total:". $rows;
$i = 0;
while ($i < $rows)
{
$db _name[$i] = Mysql_tablename ($dbs, $i);
echo $db _name[$i];
$i + +;
}
?>
You can display all of the database names in MySQL, in turn.
Note: Equivalent to the show databases command in MySQL
2, Mysql_list_tables ()
Format: int mysql_list_tables (string database name);
Displays the names of all tables under the database, table name.
You can display the names of all the tables under MySQL in turn
Note: Equivalent to the show Tables command in MySQL (use MySQL command to select 1 databases first)
(Source: Wind Flash)
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.