Phpmysql: Why is the code behind the successful execution of the destructor after the problematic database connection?

Source: Internet
Author: User
Tags mysql connect
Phpmysql: Why is the code behind the successful execution of the destructor after the problematic database connection? /*
* Class mysql
*/
Class mysql_class
{
Public $ host;
Public $ root;
Public $ passwd;
Public $ database;
Public $ ut;
Public $ link;

/// Construct
Function _ construct ($ host, $ root, $ passwd, $ database, $ ut)
{
$ This-> host = $ host;
$ This-> root = $ root;
$ This-> passwd = $ passwd;
$ This-> database = $ database;
$ This-> ut = $ ut;
$ This-> connect ();
}

/// Destruct
Function _ destruct ()
{
Echo $ this-> link;
Mysql_close ($ this-> link );
Echo "destruct
";
}

/// Mysql connect
Function connect ()
{
$ This-> link = mysql_connect ($ this-> host, $ this-> root, $ this-> passwd );
If (! $ This-> link)
{
Die ("cocould not connect". mysql_error ()."
");
}
Else {
Echo "Connect successed
";
}
Mysql_select_db ($ this-> database, $ this-> link) or die ("No Database:". $ this-> database ."
");
Mysql_query ("set name 'utf8 '");
}
}

//// Execute the following test1
//// Or execute the following test2


?>
Run test1: code
/// Test1
$ Ms = new mysql_class ("localhost", "admin", "admin", "php1000", "UTF8 ");
$ Ms = null;
Echo'
Mysqlclass end =
';
$ Con = mysql_connect ("localhost", "admin", "admin ");
If (! $ Con ){
Die ('could not connect: '. mysql_error ());
}
Else {
Echo"
11 connect successed ";
}
Echo $ con. "= con
";
// Some code...
Mysql_close ($ con );
// The test1 result is as follows: (why is there no 11 connect successed Resource id #3 = con output in the result)
Connect successed
No Database: php1000
Resource id #3 destruct

Run test2:
//// Test2 code
$ Con = mysql_connect ("localhost", "admin", "admin ");
If (! $ Con ){
Die ('could not connect: '. mysql_error ());
}
Else {
Echo"
11 connect successed
";
}
Echo $ con. "= con
";
// Some code...
Mysql_close ($ con );
$ Ms = new mysql_class ("localhost", "admin", "admin", "php1000", "UTF8 ");
$ Ms = null;
Echo'
Mysqlclass end =
';
// Test2 result:
11 connect successed
Resource id #3 = con
Connect successed
No Database: php1000
Resource id #5 destruct


Reply to discussion (solution)

Mysql_select_db ($ this-> database, $ this-> link) or die ("No Database:". $ this-> database ."
");
Since No Database: php1000 is output
It indicates that your program has ended ahead of schedule, and the subsequent content is not executed, so naturally there is no output.

Thank you for your reminder.

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.