Home >php教程 >php手册 >PHP错误Warning:mysql_query()的解决方法

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-13 11:36:401357browse

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

代码:

Copy to Clipboard引用的内容:[www.bkjia.com] 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