Home  >  Article  >  php教程  >  PHP错误Warning:mysql_query()解决方法,warningmysql_query

PHP错误Warning:mysql_query()解决方法,warningmysql_query

WBOY
WBOYOriginal
2016-06-13 08:52:451373browse

PHP错误Warning:mysql_query()解决方法,warningmysql_query

php提示错误:Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO)
代码:

<&#63;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"); 

原因:
mysqlClass第二次初使化时,先初使化mysqlClass,得到跟第一个$db相同的$this->link,然后调用__construct函数会把this->link关闭。
最后导致$db中mysql资源为空,弹出错误。
解决办法:
$db=$db?$db:new mysqlClass(); 
或者
$this->link=mysql_connect($host,$user,$pw,true);

希望提供的解决方法可以真正的帮助到大家。

Statement:
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