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.