Home > Backend Development > PHP Tutorial > php mysql问题 数据库连接成功执行析构函数后 后面的代码不执行什么原因

php mysql问题 数据库连接成功执行析构函数后 后面的代码不执行什么原因

WBOY
Release: 2016-06-23 14:40:02
Original
1093 people have browsed it

/*
 * 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


回复讨论(解决方案)

mysql_select_db($this->database,$this->link) or die(" No Database:".$this->database."
");
既然输出了 No Database:php1000
就表示你的程序提前结束了,后面的内容没有执行,自然就没有输出

谢谢楼上大神的提醒

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template