0

Hi All, I am trying to connect to a database OOP singleton method.

I am getting an error "syntax error, unexpected 'function_construct'" and I unable to find the syntax error in the code.

Any help with this would be appreciated.

Thanks in advance

David

// Page: index.php

<?php
require_once 'classes/db.php';

echo '<pre>', print_r(DB::getInstance()->query('SELECT * FROM Users')->results()), '</pre>';

//classes/db.php

 <?php
                    //classes/db.php
    class DB 
    {
        private $_mysqli,
        $_query,
        $_results = array(),
        $_count=0;

        public static $instance;

        public static function getInstance(){
            if (isset(self::$instance)){
                self::$instance = new DB();
            }
            return self::$instance;
        }

        public function_construct(){
            $this->_mysqli = new mysqli('localhost','xxxx','xxxxx','db301847341');
            if($this->_mysqli->connect_error) {
                die($this->_mysqli->connect_error);
            }
        }

        public function query($sql){
            if($this->_query= $this->_mysqli->query($sql)){
                while ($row = $this->_query->fetch_object()){
                    $this->_results[] = $row;
                }
            }
            return $this;
        }

        public function result(){
            return $this->_results;

        }
    }

Edited by davidjennings

2
Contributors
5
Replies
13
Views
3 Years
Discussion Span
Last Post by davidjennings
0

Thank you Pritaeas, That worked and I get the following error in addition?

Fatal error: Call to a member function query() on a non-object in C:\wamp\www\db\index.php on line 4

Any help would be appreciated

Thanks

David

0

I have just made an amendment to line 35 to read

 public function results(){
            return $this->_results;

        }

But the problem is still occuring

Thanks in advance

David

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.