PHP4 and MySQL database operation function detailed explanation (ii)

Source: Internet
Author: User
Tags format connect mysql database

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



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.