link=mysql_connect($host,$user,$pw, true);"."/> link=mysql_connect($host,$user,$pw, true);".">
Home >Backend Development >PHP Problem >How to solve php mysql_query error problem
php mysql_query error solution: 1. Use the code "$db=$db?$db:new mysqlClass();"; 2. Use the code "$this->link=mysql_connect($host ,$user,$pw,true);”.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
PHP error Warning: mysql_query() solution Method
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- is obtained as the first $db. >link, and 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);
[Recommended learning: PHP video tutorial]
The above is the detailed content of How to solve php mysql_query error problem. For more information, please follow other related articles on the PHP Chinese website!