PHP and MySQL (9)

Source: Internet
Author: User
PHP and MySQL getting started (9) because MySQL is a completely distributed software, we must consider these possibilities: service unavailability, network congestion, or mismatched user names and passwords. In these cases, the mysql_connect function cannot return a connection identifier (because the connection is not established ). In this case, a logical false is returned. This allows us to use an if statement to process the connection:

$ Dbcnx = @ mysql_connect ("localhost", "root", "mypasswd ");
If (! $ Dbcnx ){
Echo ("

Unable to connect to ".
"Database server at this time.

");
Exit ();
}

Three new things appear in the above code segment. First, we added a @ symbol before the mysql_connect function. Many functions, including mysql_connect, will display ugly error messages after failure. Adding a @ symbol before the function name can tell this function to display our friendly error information when execution fails.

Second, an exclamation point is added before the $ dbcnx variable in the conditions of our if statement. This exclamation point is the "no operator" in PHP ". That is to say, it turns logical truth into logical false and logical false into logical truth. In this case, if the connection fails, mysql_connect will return a logical false ,! $ Dbcnx equals to logical truth, so that our if statement will be executed. On the contrary, if the connection is successful, the connection ID stored in $ dbcnx will be equal to the logical truth (in PHP, any non-zero number is considered logical truth), so! $ Dbcnx equals to logical false, and if statements are not executed.

The last one is the exit function, which is the first function without parameters. All the functions of this function are to cause PHP to stop reading this page. If the database connection fails, this is a good response, because in most cases, if you cannot connect to the database, this page does not display any useful information.

As we have done in chapter 2, the next step after the connection is established is to select the database to work on. We will work in the joke database established in chapter 2. This database is named jokes. Another function used to select a database in PHP:

Mysql_select_db ("jokes", $ dbcnx );

Note that the $ dbcnx variable is used to indicate the connection id used by this function. This parameter can be omitted. If this parameter is omitted, the function automatically uses the last enabled connection. If the function succeeds, the system returns logical truth. if the function fails, the system returns logical false. To be cautious, we also use an if statement to handle errors:

If (! @ Mysql_select_db ("jokes ")){
Echo ("

Unable to locate the joke ".
"Database at this time.

");
Exit ();
}

After establishing a connection and selecting a database, we can start to use the data stored in the database.

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.