How are you calling the DBController? I assume this isn't laravel related
Jan 8, 2022
13
Level 5
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.
Level 122
@Deekshith put the include in the __construct method
1 like
Please or to participate in this conversation.