doumin4553 2013-03-31 01:58
浏览 21
已采纳

访问类中的数据库构造[关闭]

I have been experimenting with persistent data from a class construct, I am trying to make a persistent connection through my public __construct, so I can use my database connection information throughout my class. My code is as followed:

class Inbox {
   # private $Database;
    public function __construct() {
        $this->$Database = new mysqli ('localhost','root','xxx','test');
    }
    public static function TestConnect (){
        if (self::Database){
            echo "Success When Connecting To Database";
        }else{
            echo "Problems When Connecting To Database";
        }
    }
    public static function TestSelect(){
        $Select = $this->Database->prepare("SELECT * FROM test");
        $Select->execute();
        $Select->bind_result($Test_ID, $Test_UName);
        $Select->fetch();

        print_r($Select);
    }
}

$Foo = new Inbox();
$Foo->TestSelect();

Now. When running

I am presented with the following errors:

Notice: Undefined variable: Database in C:\xampp\htdocs\InboxFeature\Api\Class.php on line 15

Fatal error: Cannot access empty property in C:\xampp\htdocs\InboxFeature\Api\Class.php on line 15


I change my lines to:

$Select = self::Database->prepare("SELECT * FROM test");

and uncomment: private $Database;

I get presented with the error:

Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR) in C:\xampp\htdocs\InboxFeature\Api\Class.php on line 25

So my overall question, is where exactly have I gone wrong, I have looked at examples shown on Stackoverflow with using the __construct() method. I can't see where it's gone wrong for me.


Using the Answer provided. I appended the following changes:

 private static $Database;
    public function __construct() {
        self::$Database = new mysqli ('localhost','root','xxx','test');
    }
    public static function TestSelect(){
        $Select = self::$Database->prepare("SELECT * FROM test");
        $Select->execute();
        $Select->bind_result($Test_ID, $Test_UName);
        $Select->fetch();

        echo $Test_ID;
    }

This has successfully worked. Thank you for the quick response

  • 写回答

1条回答 默认 最新

  • dongqiaochi2711 2013-03-31 02:05
    关注

    You have the syntax reversed.

    If you declare the property as private $database; then you would access it using $this->database, not $this->$database.

    If you instead declare it as a static property (private static $database) then it would be self::$database, not self::database.

    Either way, you should make sure the property is declared at the top of the class; in your example you have private $database commented out.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊