Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Deekshith's avatar

php construct error

i have core php connect.php file like below,

<?php
// Enable us to use Headers
ob_start();

// Set sessions
if(!isset($_SESSION)) {
    session_start();
}
// header("Access-Control-Allow-Origin: *");
// header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "sample";
date_default_timezone_set('Asia/Kolkata');

try {
    $conn = new \PDO('mysql:dbname=ltc;host=localhost;port=3306', $username, $password);
    // set the PDO error mode to exception
	$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

} catch (\PDOException $Exception) {
    echo $Exception->getMessage();
    die;
}

?>

And now i have a file like below,

<?php
include '../includes/connect.php';
class DBController {

	private $db;
	
    function __construct($conn) {
        $this->db = $conn;
	}	


    function runBaseQuery($query)
    {
        $result = $this->db->query($query);
        while ($row = $this->db->fetch(PDO::FETCH_ASSOC)) {
            $resultset[] = $row;
        }
        if (!empty($resultset))
            return $resultset;
    }

    function runQuery($query, $param_type, $param_value_array) {

        $sql = $this->db->prepare($query);
        $this->db->execute($sql, $param_type, $param_value_array);
        $result = $this->db->fetchAll(PDO::FETCH_ASSOC);

        if ($result->num_rows > 0) {
            while ($row = $this->db->fetchAll(PDO::FETCH_ASSOC)) {
                $resultset[] = $row;
            }
        }
        if(!empty($resultset)) {
            return $resultset;
        }
    }

But above code giving me below errror,

Fatal error: Uncaught ArgumentCountError: Too few arguments to function DBController::__construct(), 0 passed in login.php on line 6 and exactly 1 expected in DBController.php:11 Stack trace: #0 login.php(6): DBController->__construct() #1 {main} thrown in DBController.php on line 11

How to use connect.php $conn variable in class file? please help me. Thank you.

0 likes
13 replies
Sinnbeck's avatar

How are you calling the DBController? I assume this isn't laravel related

Deekshith's avatar

@Sinnbeck Thank you for the login reply. this is not laravel. this is core php i have login.php file in login.php i am calling like below,

function getMemberByEmail($email) {
        $db_handle = new DBController();
        $query = "select * from admin_user where email = :email LIMIT 1";
        $result = $db_handle->runQuery($query, 's', array($email));
        return $result;
    }

I think i should pass $conn variable in new DBController?

Snapey's avatar

put the connection include INSIDE the DB controller class?

Deekshith's avatar

@Snapey I cant use include inside class i got error like below,

syntax error, unexpected 'include' (T_INCLUDE), expecting function (T_FUNCTION) or const (T_CONST)
Deekshith's avatar

@Snapey Great it worked i changed like below,

private $db;
	
    function __construct() {
        include '../includes/connect.php';
        $this->db = $conn;
    }	

now i can access $this->db Thank you.

Snapey's avatar

@Deekshith personally I would put the code direct in the constructor since it is probably only used by this class

1 like
Sinnbeck's avatar

@Deekshith if you are following mvc pattern, the database handling normally wouldn't be in a controller either. It would in a dedicated class or a base model

Deekshith's avatar

@Sinnbeck Yes but user end part was already developed and i am just adding admin end for already developed project. there is no MVC pattern in user end. can i create new connection only for admin end along with user end connection? i thought to use same connection file.

Sinnbeck's avatar

@Deekshith if they use the same database, then yes you can. But without seeing the full project, it's hard to suggest what is best

Deekshith's avatar

@Sinnbeck till now the project is like this

  1. User end registration form is there along with payment gateway
  2. Data will be stored in db and they are using connect.php file as a connection file .

Now i am implementing the admin flow.

what i am doing is using already created connect.php file instead of calling in class file. another option i can do is,

creating new db connection class file and using this class file for admin end queries.

Please or to participate in this conversation.