PHP mysql problem database connection after successful execution of the destructor, the code behind does not perform any reason

Source: Internet
Author: User
Tags mysql connect php mysql
/*
* 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 ("Could 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 '");
}
}

Perform the following test1
or perform the following test2


?>
Execute 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 "
11connect successed ";
}
echo $con. " ==con
";
Some code ...
Mysql_close ($con);
Test1 results are as follows: (why there is no 11connect successed Resource ID in the result #3 ==con these content output)
Connect successed
No database:php1000
Resource ID #3destruct

Execution Test2:
Test2 Code
$con = mysql_connect ("localhost", "admin", "admin");
if (! $con) {
Die (' Could not connect: '. Mysql_error ());
}
else{
echo "
11connect successed
";
}
echo $con. " ==con
";
Some code ...
Mysql_close ($con);
$ms = new Mysql_class ("localhost", "admin", "admin", "php1000", "UTF8");
$ms = null;
Echo '
Mysqlclass end==
';
Test2 results:
11connect successed
Resource ID #3 ==con
Connect successed
No database:php1000
Resource ID #5destruct


Reply to discussion (solution)

mysql_select_db ($this->database, $this->link) or Die ("No database:". $this->database. "
");
Since the output of No database:php1000
It means that your program is finished prematurely, the content is not executed, and there is no output.

Thanks for the big God's reminder upstairs.

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