php mysql问题 数据库连接成功执行析构函数后 后面的代码不执行什么原因
程序员文章站
2022-06-04 17:05:01
...
/*
* 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
* 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
就表示你的程序提前结束了,后面的内容没有执行,自然就没有输出
谢谢楼上大神的提醒
上一篇: SQL SERVER 查询性能优化分析事务与锁(五)
下一篇: 这两种查询逻辑应该用哪个?