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;

        }
    }

Recommended Answers

All 5 Replies

public function_construct() {

Should be:

public function __construct() {

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

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

if (isset(self::$instance)) {

should be:

if (!isset(self::$instance)) {

Hi Pritaeas, as always thanks

David

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.