Marc by Marc Jacobs official website PHP4 and MySQL database operation function

Source: Internet
Author: User
Say that PHP can not mention MySQL, and 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 with PHP.
The following is a detailed analysis of the functions of MySQL-related operations in PHP4 (a total of 32, beginning with Mysql_):
<1>. Functions that connect to the database server (2):
(1). mysql_connect ()
Format: int mysql_connect (string [hostname] [ort],string [username],string [password]);
The port parameter in the parameter represents the port number of the database server, which is usually the default port number.
If you do not fill in any parameters, the default hostname is Localhost,username Root,password is empty.
The function executes successfully, returns a connection number of type int (link_identifier), fails execution, and returns 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, such as mysql_connect () execution fails, the system's error is displayed, and then the execution continues down. So, how do you block these system error prompts and end the program after a failure?
In MySQL, it is allowed to precede the database function with the @ symbol, to mask the system's error, and to use the Die () function to give easier-to-understand error hints, and the Die () function will automatically exit the program.
The above example can be changed to:
$connect = @mysql_connect ("localhost", "User", "password") or Die ("Unable to connect database server!");
?>
If the mysql_connect () execution fails, the program is exited after the unable to connect database server! is displayed.
(2). Mysql_pconnect ()
Format: int mysql_pconnect (string [hostname] [ort],string [username],string [password]);
This function is basically the same as (1) mysql_connect (), except that:
---------when the database operation is completed, the connection established by mysql_connect (1) is automatically closed, and (2) the connection established by Mysql_pconnect () will continue to exist, which is a stable and persistent connection.
---------(2) mysql_pconnect (), before each connection, will check if there is a connection using the same hostname,use,password, and if so, use this connection number directly.
The connection established by the mysql_connect () of the---------(1) can be closed with mysql_close (), while (2) mysql_pconnect () cannot be closed with mysql_close ().
&LT;2&GT; Close the database connection function (1):
Mysql_close ()
Format: int mysql_close (int link_identifier);
Closes the connection established by the mysql_connect () function, executes successfully, returns the Ture value, and fails with a value of false.
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.
&LT;3&GT; 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), 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, returns a value of false.
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, and it is meaningless to use it alone!
2, Mysql_db_query ()
Format: int mysql_db_query (String database, string sqlquery, int link_identifier);
In this function, you must specify the database name databases and the SQL statement sqlquery, which returns false if it fails.
Example:
$connect = mysql_connect ($hostname, $user, $pwd);
$query = Mysql_db_query ($dbname, $sql, $connect);
if ($query) echo "Successed!";
else echo "Failed!";
?>
The difference between mysql_db_query () and mysql_query () is that the former can select the database without using mysql_select_db (), while executing the SQL statement and selecting the database.
<5> Database record operation function (5):
1, Mysql_fetch_array ()
Format: array mysql_fetch_array (int query);
Executes successfully, returning an array that holds the value of the next record, if execution fails, and returns a value of false.
The returned array can be represented either by subscript or by field name.
Example:
$query = mysql_query ($sql, $connect);
while ($arrary = Mysql_fetch_array ($query))
{
echo $array [Column1]. "| ". $array [Column2];
echo $array [0]. "| ". $array [1];
}
?>
Note: The subscript of the array starts from 0!
2, Mysql_fetch_row ()
Format: array = mysql_fetch_row (int query);
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 by an array subscript.
Successfully returns an array that fails 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 and starts at 0.
Another: Mysql_fetch_row () executes faster than mysql_fetch_array () and reads the next line of data.
3, Mysql_result ()
Format: int mysql_result (int query, int row, string filedname);
In Mysql_result (), the parameter row must start at 0, and the parameter filedname must be a real field name and cannot be represented by a subscript.
Executes successfully, returns the numeric value of the field fetched by the database, and returns a false value if unsuccessful.
Example:
$query = mysql_query ($sql, $connect);
Echo mysql_result ($query, 0, "Column1"). "
";
Echo mysql_result ($query, 1, "Column1"). "
";
Echo mysql_result ($query, 2, "Column1"). "
";
?>
Note: This function is less functional but easy to use.
4, Mysql_fetch_object ()
Format: Object mysql_fetch_object (int query)
Can iterate over the specified field, execute successfully, return the numeric value as object, and fail to return a value of false.
Example:
$query = mysql_query ($sql, $connect);
while ($object = Mysql_fetch_object ($query))
{
Echo $object->column1. "
";
Echo $object->column2. "
";
Echo $object->column3. "
";
}
?>
The Note:mysql_fetch_object () function returns 1 objects after a successful execution!
The operation is as follows:
$object, field name
5, Mysql_data_seek ()
Format: int mysql_data_seek (int row, int query);
Moves the cursor to the specified row (row_number)
Execution succeeds, returns True, fails, returns false value.
This function can be used in conjunction with mysql_fetch_array () or mysql_fetch_row (), that is, after using the Mysql_data_seek () function, you can use Mysql_fetch_array () or Mysql_fetch_ The row () function to display the specified line.
Example:
$query = mysql_query ($sql, $connect);
$seek = Mysql_data_seek ($query, 2);
$arrary = Mysql_fetch_array ($query);
echo $array [Column1]. "
";
echo $array [Column2]. "
";
?>
<6&gt. Database-level operation functions (2):
1, mysql_create_db ()
Format: int mysql_create_db (string database name, int link_identifier);
Using the program to build 1 databases database, of course you can also use the mysql_query () or mysql_db_query () function to create or delete
But we can use this function to more easily build 1 database.
A successful return of 1 true values, or 1 false if it fails.
Example:
$connect = mysql_connect ("$hostname", "$user", "$pwd");
$create = mysql_create_db ("dbtest", $connect);
if ($create) echo "CREATE Database dbtest successed!";
else echo "CREATE database dbtest failed!";
?>
2, mysql_drop_db ()
Format: int mysql_drop_db (string database name, int link_identifier);
A program to delete 1 databases database.
But we can use this function to more easily delete 1 database.
A successful return of 1 true values, or 1 false if it fails.
Example:
$connect = mysql_connect ("$hostname", "$user", "$pwd");
$create = mysql_drop_db ("dbtest", $connect);
if ($create) echo "drop database dbtest successed!";
else echo "drop database dbtest failed!";
?>
Note: If you are using mysql_query () or Mysql_db_query (), the SQL statement should be:
(1) Create DATABASE dbtest
(2) Drop database dbtest
7) Database information functions (2):
1, Mysql_fetch_field ()
Format: Object Mysql_fetch_field (int query, int [field_offset]);
Returns 1 objects, a hash table, with the following labels:
Table: Tables Name
Name: Field name
Max_length: Maximum length of this field
Not_null: The field is not NULL to return 1, otherwise 0 is returned
Primary_key: Field Primary key returns 1, otherwise returns 0
Unique_key: field is unique key returns 1, otherwise returns 0
Multiple_key: The field is non-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
Reference format: Object name, subscript name
Use this function to get the table name, field name, type ....
Example:
$query = mysql_query ($sql, $connect);
while ($object = Mysql_fetch_field ($query))
{
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. "
";
}
?>
Note: The hash table starts with 0 coordinates, that is, the first field is the 0 item in the Hashtable.
If we want to directly get information about the third field of the hash table, the following format can be used:
$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 purpose.
2, Mysql_field_seek ()
Format: int mysql_field_seek (int $query, int field_offset);
Moves the cursor over the specified field.
Example:
$query = mysql_query ($sql, $connect);
$seek = Mysql_field_seek ($query, 2);
$object = Mysql_fetch_field ($query);
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. "
";
?>
This also achieves 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 the available database names (databases 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 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.
Example:
$connect = mysql_connect ($host, $USR, $pwd);
$tables = Mysql_list_tables ("MySQL");
$rows = mysql_num_rows ($tables);
echo "Table total:". $rows;
$i = 0;
while ($i < $rows)
{
$table _name[$i] = Mysql_tablename ($tables, $i);
echo $table _name[$i];
$i + +;
}
?>
The names of all the tables under MySQL can be displayed in turn
Note: Equivalent to the show Tables command in MySQL (use MySQL command first to select 1 databases)

The above describes the Marc by Marc Jacobs official website PHP4 and MySQL database operation function, including the Marc by Marc Jacobs website content, I hope the PHP tutorial interested friends helpful.

  • 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.