PHP4 and MySQL database operation function detailed explanation

Source: Internet
Author: User
Tags connect mysql query create database mysql database port number
mysql| Function | data | database | detailed

Say 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):

(1). mysql_connect ()
Format: int mysql_connect (string [hostname] [:p ort],string [username],string [password]);

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:

<?php

$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:

<?php

$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] [:p 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:
<?php

$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);
Returns 1 false values if the specified database name is selected, succeeds, returns 1 Truth (True), and fails.

Example 1:
<?php
$select = mysql_select_db (' forum ', $connect);
if ($select)
{echo "Connect db forum successed!";}
Else
{echo "Connect db forum failed!";}
?>

Example 2:
<?php
$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:
<?php
$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.

Example:

<?php
$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 database databases without using mysql_select_db (), while the SQL statement is executed while the database is selected.


<5>. Database record operation function (5):


1, Mysql_fetch_array ()
Format: array mysql_fetch_array (int query);

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.

Example:
<?php
$query = mysql_query ($sql, $connect);
while ($arrary = Mysql_fetch_array ($query))
{

echo $array [Column1]. "| ". $array [Column2];
echo $array [0]. "| ". $array [1];

}
?>
Note: The subscript for the array starts at 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 as an array subscript.
A successful return of an array failed to return a value of false.

Example:
<?php
$query = mysql_query ($sql, $connect);
while ($row = Mysql_fetch_row ($query))
{
echo $row [0]. " | " . $row [1]. "<br>";
}
?>
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.

Example:
<?php
$query = mysql_query ($sql, $connect);
Echo mysql_result ($query, 0, "Column1"). " <br> ";
Echo mysql_result ($query, 1, "Column1"). " <br> ";
Echo mysql_result ($query, 2, "Column1"). " <br> ";
?>

Note: This function is less functional but easy to use.

4, Mysql_fetch_object ()
Format: Object mysql_fetch_object (int query)

Can iterate over a specified field, perform successfully, return a value as Object objects, and return a value of false.

Example:
<?php
$query = mysql_query ($sql, $connect);
while ($object = Mysql_fetch_object ($query))
{
Echo $object->column1. "<br>";
Echo $object->column2. "<br>";
Echo $object->column3. "<br>";
}
?>


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.

Example:
<?php
$query = mysql_query ($sql, $connect);
$seek = Mysql_data_seek ($query, 2);
$arrary = Mysql_fetch_array ($query);
echo $array [Column1]. <br> ";
echo $array [Column2]. <br> ";
?>


<6>. Database-level operation functions (2):


1, mysql_create_db ()
Format: int mysql_create_db (string database name, int link_identifier);

Through the program to build 1 database databases, of course, you can also use the mysql_query () or mysql_db_query () function to create or delete db

But we can use this function more easily to build 1 database.
1 true values were successfully returned, and 1 false were returned for failure.

Example:

<?php

$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 database databases.

But we can use this function to more easily delete 1 database.
1 true values were successfully returned, and 1 false were returned for failure.

Example:

<?php

$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 use mysql_query () or Mysql_db_query (), the SQL statement should be:
(1) Create DATABASE dbtest
(2) Drop database dbtest




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.