0

I've recently encountered an error in my code:

PHP Parse error:  syntax error, unexpected '$connection' (T_VARIABLE), expecting function (T_FUNCTION) in

From what I've googled the issue is not setting public/private/protected before, or not creating the database connection in the construct function.

The database connection class code:

 class databaseConnection {


    //Database information
    protected $mysqliUser = "";
    protected $mysqliHost = "localhost";
    protected $mysqlipass = "";
    protected $mysqlidbname = "";
    public $con;


    public function __construct() {
        $this->con = new mysqli($this->mysqliHost,$this->mysqliUser,$this-
        >mysqlipass,$this->mysqlidbname); 

    }

}

A snippet of the class code that is receiving the error:

private $conn;
    $conn = new databaseConnection;

Any help to point me in the right direction is much appreciated.

10
  • 1
    Remove public $this->connection and your return statement in your __construct. __construct method can't return a value. Commented Aug 26, 2017 at 16:52
  • 2
    It sure can, but it shouldn't. Commented Aug 26, 2017 at 16:55
  • @cale_b yeah hahaha, just easier to paste snippets rather than having to remove private information. Commented Aug 26, 2017 at 17:30
  • @Marwelln ah thanks, however sadly still receiving the same error. Commented Aug 26, 2017 at 17:36
  • "Unexpected" usually means something wrong with the thing before it, but we can't see that code. (additional notes, it's arguably pointless returning as you are. And while not necessary, it's usually best practice to use parenthesis when instantiating a new class.) Commented Aug 26, 2017 at 17:43

2 Answers 2

1

above the private declaration is just the class creation class createSession

In which case your $conn = new databaseConnection; either needs visibility, or should be in a method. Hard to tell which without seeing all the class and the odd indentation you have.

So put that in the method it belongs in (perhaps the constructor?) or set it to private, protected, or public - whichever it was intended for.

Sign up to request clarification or add additional context in comments.

Comments

0

Check this out.

It really works..

connect.php

class DBConnect {
    private static $connection;
    private static $host = "localhost";
    private static $user = "root";
    private static $pwd = "";
    private static $dbname = "yourDBName";

    public static function connect() {
        $host = self::$host;
        $user = self::$user;
        $pwd = self::$pwd;
        $dbname = self::$dbname;

        self::$connection = new mysqli($host, $user, $pwd, $dbname) or die('Error connecting..');
        return self::$connection;
    }
}

myclass.php

require_once('connect.php');

class MyClass {
    private $connObj;

    public function __construct() {     
        $this->connObj = DBConnect::connect();
    }

    public function select() {
        $conn = $this->connObj;
        $sql = "SELECT * FROM table_name";

        $query = $conn->query($sql);
        $res = $query->fetch_assoc();
        // your code..
    }
}

$obj = new MyClass();
$obj->select();

Hope, this code snippet works fine for you. :) :)

3 Comments

Well apart from this not really helping the OP from fixing their own code ... You shouldn't use die() in such places, should be handled better. Especially with uninformative output which is bad for users. You shouldn't use raw SQL, parametrise instead. You shouldn't select * (debatable, but meh)...+ other things...sorry but this is old code :( "it really works" - so does beating a screw with a hammer, but, screwdrivers ftw ;)
Thanks @james but OP main issue was to retireval of connection object from other class. Old is Gold and.. anyway i know about SQL Injection and it's best practices.. it was not rather necessary to include all the stuff just tried to help OP to sort out his main problem
well that's why I didn't downvote ;) old is not gold imo, easy to write, and nothing new to learn, but maintenance nightmare and hard to work with in the long run. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.