php - 为什么会弹出 'database not selected'错误?

标签 php mysql

看一下这段代码

include_once("includes/config.php");
include_once("includes/classes.php");
include_once("includes/functions.php");
$db = new MySQLDatabase();
function do_something(){
    $db = new MySQLDatabase();
    unset($db);
}
do_something();
$db->query = "SELECT * FROM users";
$db->query();
$result = $db->fetch_array();
var_dump($result);
echo"</pre>";
unset($db);

你只需要在所有包含的classes.php中看到MySQLDatabase类,这就是它

class MySQLDatabase{
        public $query = "";
        private $last_query = "";
        private $query_result;
        private $connection;
        function __construct(){
            $this->connection = mysql_connect(DB_SERVER,DB_USER_NAME,DB_PASSWORD);
            $db_select = mysql_select_db(DB_NAME,$this->connection);
            if((!$this->connection) || (!$db_select)){
                die("can't connect to database<br/> ERROR : ".mysql_error());
            }
        }
        public function get_last_query(){
            return $this->last_query;
        }
        private function escape_query(){
            mysql_real_escape_string($this->query);
        }
        public function query(){
            $this->escape_query();
            $this->query_result = mysql_query($this->query);
            $this->check_result();
            $this->last_query = $this->query;
            return $this->query_result;
        }
        private function check_result(){
            if(!$this->query_result){
                die("mysql query failed<br/>query :".$this->query."<br/> Eror :".mysql_error()."<br>".$this->last_query."<br>");
            } else{
                return 1;
            }
        }
        public function fetch_array(){
            $ass_array = array();
            while($temp = mysql_fetch_array($this->query_result)){
                array_push($ass_array,$temp);
            }
            return $ass_array;
        }

        public function num_rows(){
            if(is_bool($this->query_result)){
                return $this->query_result;
            } else{
            return mysql_num_rows($this->query_result);
            }
        }

        public function affected_rows(){
            //return mysql_affected_rows($this->query_result);
            if(is_bool($this->query_result)){
                return $this->query_result;
            } else{
            return mysql_affected_rows($this->query_result);
            }
        }

        public function close_connection(){
            mysql_close($this->connection);
        }
        function __destruct(){
            $this->close_connection();
        }
    }

我的问题是
当我尝试创建 MySQLDatabase($db) 类的新对象时,为什么它会与全局 $db 混淆,我的意思是在函数中取消设置($db)时,它正在取消设置全局 $db 并且我得到错误如下所示 error

即使我更改函数中的变量名称,此错误仍然出现?

include_once("includes/config.php");
include_once("includes/classes.php");
include_once("includes/functions.php");
$db = new MySQLDatabase();
function do_something(){
    $dbl = new MySQLDatabase();
    unset($dbl);
}
do_something();
$db->query = "SELECT * FROM users";
$db->query();
$result = $db->fetch_array();
var_dump($result);
echo"</pre>";
unset($db);



我想到的可能的解决方案:
在此代码中,我尝试与数据库建立两个并行连接,一个使用 $db 对象,另一个使用 $dbl 对象,可能 MySQL 无法建立两个并行连接!

最佳答案

您遇到的问题与一般变量范围无关。 PHP的变量范围是very well-defined .

您的问题源于您调用mysql_queryMySQLDatabase::query 中,没有函数的第二个参数(“链接标识符”)。虽然参数是可选的,according to the documentation :

If the link identifier is not specified, the last link opened by mysql_connect() is assumed.

由于您上次打开的数据库链接(do_something 中的数据库链接)已关闭,PHP 在后续调用 mysql_query 时不再有可引用的数据库链接。

尝试更新此行:

$this->query_result = mysql_query($this->query);

要明确引用类中的数据库链接属性:

$this->query_result = mysql_query($this->query, $this->connection);

关于php - 为什么会弹出 'database not selected'错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20810710/

相关文章:

php - 从一个给定的字符按列排序mysql数据库

java - 使用 mysql 驱动程序使用 mysql 数据库设置 spring-boot 项目

php - Laravel - Group By 查询错误,适用于 phpmyadmin 和结果

php - mySQL 查询,分组依据然后按最近分组的顺序排序?

javascript - 在 jQuery 中通过 AJAX 发送表单,然后保存在 MySQL 中,但不起作用

php - 什么时候使用 CMS 构建网站是好主意还是坏主意

php - 为什么人们不使用序号来存储图像?

php - "simple"CollectionType 包含错误

MySQL 数据库中的 HTML -- 最佳实践

mysql - 高效的短SQL查询