Home > Article > Backend Development > Detailed explanation of PHP4 and MySQL database operation functions_PHP tutorial
I
When talking about PHP, you cannot fail to mention MySQL, and if you want to talk about MySQL, then PHP must also be mentioned. The rapid rise of PHP is inseparable from MySQL, and the widespread application of MySQL is also closely related to PHP.
The following is a detailed analysis of the functions related to MySQL in PHP4 (32 in total, all starting with mysql_):
<1>. Functions to connect to the database server (2 ):
(1).mysql_connect()
Format: int mysql_connect(string [hostname] [ort],string [username],string [password]);
Parameters The port parameter in represents the port number of the database server. Generally, its default port number can be used.
If you do not fill in any parameters, the default hostname is localhost, username is root, and password is empty.
If the function is executed successfully, it will return a link_identifier of type int. If it fails, it will return a false value.
Example:
$connect = mysql_connect("localhost","user","password");
if($connect) echo "Connect Successed!"; //Connect successfully, display Connect Successed!
else echo "Connect Failed!"; //Connect failed, display Connect Failed!
?>
In the above example, if mysql_connect() fails to execute, the system error message will be displayed, and then execution will continue. So, how to block these system error prompts and end the program after failure?
In MySQL, it is allowed to add the @ symbol before the database function to block the system error prompt. At the same time, the die() function is used to give a more understandable error prompt, and then 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 mysql_connect() fails to execute, Unable to connect database server! will be displayed and the program will exit.
(2).mysql_pconnect()
Format: int mysql_pconnect(string [hostname] [ort],string [username],string [password]);
This function is the same as (1) mysql_connect() is basically the same, the difference is:
--------- When the database operation is completed, the connection established by mysql_connect() of (1) will be automatically closed, while (2) The connection established by mysql_pconnect() will continue to exist and is a stable and durable connection.
--------- In mysql_pconnect() in (2), before each connection, it will check whether there is a connection using the same hostname, use, password. If there is, use this connection number directly. .
--------- The connection established by mysql_connect() in (1) can be closed with mysql_close(), while mysql_pconnect() in (2) cannot be closed with mysql_close().
<2>. Close database connection function (1):
mysql_close()
Format: int mysql_close(int link_identifier);
Close by mysql_connect The connection established by the () function returns a true value if the execution is successful, and a false value if it fails.
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 be closed by mysql_pconnect( ) function establishes the connection.
<3>. Select database function (1):
mysql_select_db()
Format: int mysql_select_db(string database name, int link_identifier);
Select the specified database name, if successful, return 1 true value (True), if failed, return 1 False value
Example 1:
$select = mysql_select_db('forum', $connect);
if($ select)
{echo "connect db forum successful!";}
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 USE in MySQL Statement: Such as USE forum
<4>.SQL query function (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 makes no sense to use it alone!
2. mysql_db_query()
Format: int mysql_db_query(string database, string sqlquery, int link_identifier);
The database name database and SQL statement sqlquery must be specified in this function, such as Returns False on failure.
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 does not need to use mysql_select_db( ) to select the database database, and select the database while executing the SQL statement.
<5>. Database record operation functions (5):
1. mysql_fetch_array()
Format: array mysql_fetch_array(int query);
Execute successfully, return 1 array , this array stores the value of the next record. If the execution fails, a False value is returned.
The returned array can be represented by either a subscript or a 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 subscripts of arrays start from 0!
2. mysql_fetch_row()
Format: array = mysql_fetch_row(int query);
has basically the same function as the mysql_fetch_array() function of 1. The difference is that mysql_fetch_row() can only be represented by array subscripts.
Returns an array on success, and returns a False value on failure.
Example:
$query = mysql_query($sql, $connect);
while($row = mysql_fetch_row($query))
{
echo $row[0] . " | " . $row[1] . "
";
Another: mysql_fetch_row() executes faster than mysql_fetch_array(), and both reads the next row of data.
3. mysql_result()
Format: int mysql_result(int query, int row, string filedname);
In mysql_result(), the parameter row must start from 0, and the parameter filedname It must be a real field name and cannot be represented by a subscript.
If the execution is successful, the value of the field retrieved from the database will be returned. If it fails, a False value will be returned.
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 has few functions, but is easy to use.
4. mysql_fetch_object()
Format: object mysql_fetch_object(int query)
Can read the specified field in a loop. If the execution is successful, the value will be returned in the form of object. On failure, a False value is returned.
Example:
$query = mysql_query($sql, $connect);
while($object = mysql_fetch_object($query))
{
echo $object-> column1 . "
";
echo $object->column2 . "
"; >
Note: The mysql_fetch_object() function returns an object 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)
If the execution is successful, a true value will be returned. If it fails, a False value will be returned.
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 the mysql_fetch_array() or mysql_fetch_row() function to display the specified row.
Example:
$query = mysql_query($sql, $connect);
$seek = mysql_data_seek($query, 2);
$arrary = mysql_fetch_array($query);
echo $array[column1]."
";
echo $array[column2]."
"; Database operation functions (2):
1. mysql_create_db()
Format: int mysql_create_db(string database name, int link_identifier);
Create a database database through the program , of course you can also use the mysql_query() or mysql_db_query() function to create or delete a database
But we can use this function to create a database more conveniently.
Returns 1 true value if successful, and 1 false value if failed.
Example:
$connect = mysql_connect("$hostname","$user","$pwd");
$create = mysql_create_db("dbtest", $connect);
if($create) echo "create database dbtest successful!";
else echo "create database dbtest failed!";
?>
2. mysql_drop_db()
Format: int mysql_drop_db(string database name, int link_identifier);
Delete a database database through a program.
But we can use this function to delete a database more conveniently.
Returns 1 true value if successful, and 1 false value if failed.
Example:
$connect = mysql_connect("$hostname","$user","$pwd");
$create = mysql_drop_db("dbtest", $connect);
if($create) echo "drop database dbtest successful!";
else echo "drop database dbtest failed!";
?>
Note : If using mysql_query() or mysql_db_query(), the SQL statement should be:
(1)create database dbtest
(2)drop database dbtest
7) Database information function (2):
1. mysql_fetch_field()
Format: object mysql_fetch_field(int query, int [field_offset]);
returns 1 object, that is A hash table with subscripts:
table: table name
name: field name
max_length: the maximum length of the field
not_null: 1 is returned if the field is not null, otherwise 0 is returned
primary_key: Returns 1 if the field is primary key, otherwise returns 0
unique_key: Returns 1 if the field is unique key, otherwise returns 0
multiple_key: Returns 1 if the field is non-unique key, otherwise returns 0
numeric: Returns 1 if the field is numeric, otherwise returns 0
blob: Returns 1 if the field is blob, otherwise returns 0
type: Type of field
unsigned: Returns 1 if the field is unsigned, otherwise returns 0
zerofill: Returns 1 if the field is zero filled, otherwise returns 0
The reference format is: object name->subscript name
Use this function to get the table name and 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 from the 0 coordinate, that is, the first field is the 0 item in the hash table.
If we want to directly get the information of the third item or field of the hash table, we 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, the same purpose can also be achieved through the following function.
2. mysql_field_seek()
Format: int mysql_field_seek(int $query, int field_offset);
Move the cursor to 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 meets the same requirements as the above example.
8) Get the database name and table name (2):
1. mysql_list_dbs()
Format: int mysql_list_dbs(int link_identifier);
Get all available database names (database 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++;
}
?>
to display all database names in MySQL in sequence ( database name).
Note: Equivalent to the show databases command in MySQL
2. mysql_list_tables()
Format: int mysql_list_tables(string database name);
Display 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++;
}
?>
can be displayed in sequence The names of all tables under mysql
Note: Equivalent to the show tables command in MySQL (first use the use mysql command to select a database)
(Source: Feng Shan)