php - 使用 OOP PHP 的 mysql.php 中出现 fatal error : Cannot access empty property,

标签 php mysql

帮助我得到这个错误信息:

Fatal error: Cannot access empty property in D:\Installed Programs\deshoppy with joomla\htdocs\ds\features\mysql.php on line 64

下面是 mysql.php:

<?php
// Give access to db information
global $db_type;
global $db_host;
global $db_user;
global $db_pass;
global $db_name;

// Connecting database
$db=mysql_connect($db_host,$db_user,$db_pass);
$db or die("Failed to connect database using $db_type :P");

// Selecting database
mysql_select_db($db_name) or
    die('Failed to select database :P');

// Non OOP Functions

function db_query($q){
    $qry=mysql_query($q);
    return $qry;
}

function db_fetch_array($qry){
    $qary=mysql_fetch_array($qry);
    return $qary;
}

function db_num_rows($qry){
    $qrnr=mysql_num_rows($qry);
    return $qrnr;
}

function db_error(){
    $db_error=mysql_error();
    return $db_error;
}

function db_close(){
    $db_close=mysql_close();
    return $db_close;
}

// OOP

class aDatabase{


    public $hostname="";
    public $username="";
    public $password="";
    public $database="";

    public $query=""; // Last SQL code
    public $result=""; // Result of mysql_query
    public $fetch_array=""; // Result of mysql_fetch_array
    public $link=""; // Result of mysql_connect
    public $num_rows=""; // Result of mysql_num_rows
    public $error=""; // Result of mysql_error
    public $disconnect=""; // Result of mysql_close

    // Adding parameter to class
    public function __construct($par_host, $par_user, $par_pass, $par_db){
        $this->$hostname=$par_host;
        $this->$username=$par_user;
        $this->$password=$par_pass;
        $this->$database=$par_db;

    }

    // Connect to database
    function connect(){
        $this->$link = mysql_connect(
            $this->$hostname,
            $this->$username,
            $this->$password
        );

        return $this->$link;
    }

    // Fetch array
    function fetch_array(){
        $this->$fetch_array=mysql_fetch_array($this->$result);
        return $this->$fetch_array;
    }

    function num_rows(){
        $this->$num_rows = mysql_num_rows($this->$num_rows);
        return $this->$num_rows;
    }

    // May not accurate to last error
    function error(){
        $this->$error = mysql_error();
        return $this->$error;
    }

    function disconnect(){
        $this->$disconnect = mysql_close($this->$link);
        return $this->$disconnect;

        $this->$link="";
    }
}

// Proccess new database connection
$aDB=new aDatabase($db_host, $db_user, $db_pass, $db_name);

$aDB->connect();

// Clean database information to prevent hacking
$db_name="";
$db_user="";
$db_pass="";
$db_host="";

?>

在 ds_configuration.php 上(我之前忘了告诉这个):

<?php
// Site offline information
$site_offline=false;
$site_offline_title="Akdira";

// Database information
$db_user="root";
$db_pass="123456";
$db_name="deshoppy";
$db_host="localhost";
$db_type="mysql";

?>

你能告诉我如何解决吗?

最佳答案

查看代码顶部的这一行:global $db_host; 这是错误的。在 PHP 中,如果要在函数中使用全局变量来访问全局范围变量,则必须在函数内声明 global

您应该在创建类实例之前为变量赋值。 您没有为变量分配任何值,例如 $db_host 等等...

$db_host = "localhost";
//...
$aDB=new aDatabase($db_host, $db_user, $db_pass, $db_name);

也可以在类变量成员中试试这个:

$this->hostname

代替:

$this->$hostname
     //^

在这种情况下,通过$this 删除所有类变量成员中的$ 符号。

更新:作为您的最后一个版本,将 ds_configuration.php 包含在 mysql.php 中:

include_once('ds_configuration.php');

关于php - 使用 OOP PHP 的 mysql.php 中出现 fatal error : Cannot access empty property,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18713164/

相关文章:

php - num_rows 不工作 pdo,mysqli,总是返回 0

c# - 为什么 Entity Framework Raw Sql Query 从 MySql 返回带有不需要的值的对象?

mysql - 如何计算MySQL中的总数百分比

php - 如何从 Laravel 中没有标签 "stdClass Object"的查询中获取结果?

php - 如何更新 AWS S3 存储桶中的对象属性

javascript - 阻止结果,通过 AJAX 显示,重新加载浏览器后

php - HTML 表单默认值为空,除非用户单击按钮?

php - 如何在 PHP 中将数组从线性分布排序为正态分布?

mysql CPU 使用率高(280%)

mysql - RowDataPacket 返回未定义的值