Rumah >pembangunan bahagian belakang >tutorial php > php mysql有关问题 数据库连接成功执行析构函数后 后面的代码不执行什么原因
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'");
}
}
////执行下面test1
////或执行下面test2
?>
执行test1:代码
////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
";
// 一些代码...
mysql_close($con);
//test1结果如下:(为啥结果里没有11connect successed Resource id #3==con这些内容输出)
Connect successed
No Database:php1000
Resource id #3destruct
执行test2:
////test2 代码
$con = mysql_connect ( "localhost", "admin", "admin" );
if (! $con) {
die ( 'Could not connect: ' . mysql_error () );
}
else{
echo "
11connect successed
";
}
echo $con."==con
";
// 一些代码...
mysql_close($con);
$ms = new mysql_class("localhost","admin","admin","php1000","UTF8");
$ms = null;
echo '
mysqlclass end==
';
//test2 结果:
11connect successed
Resource id #3==con
Connect successed
No Database:php1000
Resource id #5destruct