Home  >  Article  >  Backend Development  >  marc by marc jacobs official website Detailed explanation of PHP4 and MySQL database operation functions

marc by marc jacobs official website Detailed explanation of PHP4 and MySQL database operation functions

WBOY
WBOYOriginal
2016-07-29 08:35:27882browse

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 operations in PHP4 (32 in total, all starting with mysql_):
<1>. Functions to connect to the database server (database server) (2):
(1).mysql_connect( )
Format: int mysql_connect(string [hostname] [ort],string [username], string [password]);
The port parameter in the parameter indicates the port number of the database server. Generally, its default port number is sufficient.
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 an int type connection number (link_identifier). 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 the 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 shield the system error prompts. 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 , will display Unable to connect database server!, then exit the program.
(2).mysql_pconnect()
Format: int mysql_pconnect(string [hostname] [ort],string [username],string [password]);
This function is basically the same as mysql_connect() in (1), the difference is:
--------- When the database operation is completed, the connection established by mysql_connect() in (1) will be automatically closed, while the connection established by mysql_pconnect() in (2) will continue to exist, which is a stable Persistent connection.​
--------- In mysql_pconnect() in (2), before each connection, it will check whether there is a connection using the same hostname, use, and 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 the connection established by the mysql_connect() function. If the execution is successful, a true value will be returned, otherwise it will return false value.
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 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 it fails, return a 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 The function is equivalent to the USE statement in MySQL: such as USE forum
<4>.SQL query function (2):
1, mysql_query()
Format: int mysql_query(string sqlquery, int link_identifier);
Send to the server A standard SQL statement request. 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, using it alone is meaningless!
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. If it fails, False will be returned. $ Example: c $ Connect = MySQL_CONNECT ($ Hostname, $ User, $ PWD);
$ Query = MySQL_DB_QUERY ($ dbname, $ SQL, $ Connect); cCESSED! "; "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, but selects the database while executing the SQL statement.
<5>. Database record operation function (5):
1, mysql_fetch_array()
Format: array mysql_fetch_array(int query);
Successfully executed, an array is returned, which stores the value of the next record. If execution fails, a False value is returned.
The returned array can be represented by either subscript or 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 mysql_fetch_array() is basically the same as 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] . "
";
}
?>
Note: The mysql_fetch_row() function can only be represented by array subscripts and starts from 0.
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 must be a real field name and cannot be subscripted. express.
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 it 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. If it fails, a False value will be returned.
Example:
$query = mysql_query($sql, $connect);
while($object = mysql_fetch_object($query))
{
echo $object->column1 . "
";
echo $object-& gt; column2 . "
";
echo $object->column3 . "
";
}
?>
Note: After the mysql_fetch_object() function is successfully executed, an object is returned!
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 successful, return true value, failure , returns a 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 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]."
";
?>
  <6>. Database-level database operation functions (2):
1, mysql_create_db()
Format: int mysql_create_db(string database name, int link_identifier);
Create a database database through a 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 mysql_query() or mysql_db_query() is used, 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 the subscript:
table: table name
name: field name
max_length: the maximum length of the field
not_null: returns 1 if the field is not null, otherwise returns 0
primary_key: returns 1 if the field is primary key, otherwise returns 0
unique_key: field If it is a unique key, return 1, otherwise return 0
multiple_key : If the field is a non-unique key, return 1, otherwise return 0
numeric : If the field is numeric, return 1, otherwise return 0
blob : If the field is blob, return 1, otherwise return 0
type : Type of field
unsigned : Return 1 if the field is unsigned, otherwise return 0
zerofill : Return 1 if the field is zero filled, otherwise return 0
Reference format is: object name->subscript name
Use this The function can 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 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, this can also achieve the same purpose 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: q $ query = mysql_query ($ SQL, $ Connect);
$ Seek = MySQL_Field_Seek ($ Query, 2); "Table name:". $ Object-& GT ;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: ".$row s;
$i = 0;
while($i<$rows)
{
$db_name[$i] = mysql_tablename($dbs, $i);
echo $db_name[$i];
$i++;
}
?>
All database names in MySQL will be displayed in sequence.
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++;
}
?>
The names of all tables under mysql can be displayed in sequence
Note: Equivalent to the show tables command in MySQL (first use the use mysql command to select a database)

The above introduces the detailed explanation of the PHP4 and MySQL database operation functions of marc by marc jacobs official website, including the content of marc by marc jacobs official website. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn