Home > Backend Development > PHP Problem > How to solve php mysql_query error problem

How to solve php mysql_query error problem

藏色散人
Release: 2023-03-09 07:56:02
Original
2842 people have browsed it

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");
Copy after login

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(); 
Copy after login

or

$this->link=mysql_connect($host,$user,$pw,true);
Copy after login

[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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template