php prompt error: Warning: mysql_query() [function. mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO)
Code:
<?php class mysqlClass { function mysqlClass($host='localhost',$user='root',$pw='',$db='mysql') { $this->link=mysql_connect($host,$user,$pw); mysql_select_db($db); } function query($sql){ mysql_query($sql); } function __destruct(){ mysql_close($this->link); //multi construct will cause error } // liehuo,net } $db=new mysqlClass(); $db=new mysqlClass(); $db->query("select * from user");
Reason:
When mysqlClass is initialized for the second time, mysqlClass is initialized first and the same $this->link as the first $db is obtained. Then calling the __construct function will close this->link.
Finally, the mysql resource in $db is empty and an error pops up.
Solution:
$db=$db?$db:new mysqlClass();
or
$this->link=mysql_connect($host,$user,$pw,true);
I hope the solutions provided can really help everyone.