link=mysql_connect($host,$user,$pw, true);"."/> link=mysql_connect($host,$user,$pw, true);".">

Home  >  Article  >  Backend Development  >  How to solve php mysql_query error problem

How to solve php mysql_query error problem

藏色散人
藏色散人Original
2021-03-30 09:17:392810browse

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);”.

How to solve php mysql_query error problem

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=&#39;localhost&#39;,$user=&#39;root&#39;,$pw=&#39;&#39;,$db=&#39;mysql&#39;) 
{ 
$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!

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