I suspect the problem lies somewhere in connect.php, as I can’t see anything wrong with the code you posted.
It’s considered bad practice to use global variables though, as they have a number of drawbacks - your code becomes dependent on global state (and these dependencies are not obvious unless you look at the code for the function/class) and can be easily broken (e.g. if one of your functions accidentally sets $pdo to a string, all the code that relies on that global variable will break, and the problem will be hard to debug).
A better solution is to pass in your $pdo object as a dependency to your class:
class cAantallen
{
protected $pdo;
public function __construct(\\PDO $pdo)
{
$this->pdo = $pdo;
}
public function dagAantal()
{
// ...
$dag = $this->pdo->prepare($d);
// ...
}
}
// Initialising the class
$foo = new cAantallen($pdo);
First, don’t use global. If I ever teach a PHP class I will instruct the students that using global is an auto fail of their project, even if it runs correctly. Global variables in general, and the global statement in particular, leads to complex, unmaintainable spaghetti code. Stop now.
Pass your pdo object to this class and bind it in the constructor.
class cAantallen {
protected $pdo;
public function __construct( PDO $pdo ) {
$this->pdo = $pdo
}
}
Then your functions can refer to $this->pdo rather than rely on a global variable.
As for why its not working - check the connection string. PDO is a bit odd in that it’s constructor only returns a PDO object if the connection succeeds. If it fails for any reason an execption will be thrown and null is returned.
class cAantallen{
protected $pdo;
public function __construct( PDO $pdo ){
$this->pdo = $pdo;
}
function dagAantal(){
$d = "SELECT SUM(aantal)
FROM operator_bericht_aantallen
WHERE operator_id = :operator
AND WEEKDAY(datum) = NOW()";
$dag = $this->pdo->prepare($d);
$dag->bindParam(":operator", $operator);
$dag->execute();
while($row = $dag->fetch()) {
echo $row['SUM(aantal)'];
}
return true;
}
function weekAantal(){
$w = "SELECT SUM(aantal)
FROM operator_bericht_aantallen
WHERE operator_id = :operator
AND WEEKDAY(datum) >= 0
AND WEEKDAY(datum) <= 6";
$week = $this->pdo->prepare($w);
$week->bindParam(":operator", $operator);
$week->execute();
while($row = $week->fetch()) {
echo $row['SUM(aantal)'];
}
return true;
}
}
however I get the following 2 errors:
Undefined variable: pdo in C:\wamp\www\Mysite\admin\index.php on line 9=$foo = new cAantallen($pdo);
and
Catchable fatal error: Argument 1 passed to cAantallen::__construct() must be an instance of PDO, null given, called in C:\wamp\www\Mysite\admin\index.php on line 9 and defined in C:\wamp\www\Mysite\admin\classes\cAantallen.php on line 6=$this->pdo = $pdo;
You must create a PDO object before trying to create an instance of your class with this setup.
$db = new \\PDO($connectionstring, $user, $password);
$foo = new cAantallen($db);
The catchable fatal error is because the constructor fretburner and I gave you has type hinting. This means the function will throw an error (which isn’t catchable despite the name, PHP is funny that way) if you try to pass an object that isn’t of the given class.
Hi Michael. Again thank you for the reply. I am realy lost now. The pdo object is created and is included at the top of the page. Truly sorry for my ignorance.
Hi fretburner & Michael. I have it nearly working the way I hoped for. I followed up on Michaels suggestion and included a connection just before creating an instance of my class, ie
require_once('connect.php');
$cAantallen = new cAantallen($pdo);
$cAantallen->dagAantal();
Like I said I have it nearly working. The problem I have now is to access a SESSION variable user_id, which was set in my login file. i.e.
function attempt($username, $password){
global $pdo;
$stmt = $pdo->prepare('
SELECT *
FROM operators
WHERE username = :username
LIMIT 1');
$stmt->execute(array(':username' => $username ));
if ($data = $stmt->fetch( PDO::FETCH_OBJ )) {
if (password_verify($password, $data->password)) {
$_SESSION['user_id'] = $data->operator_id;
$_SESSION['user'] = $data->naam;
$_SESSION['username'] = $data->username;
return true;
} else {
return false;
}
} else {
return false;
}
}
How can I get $_SESSION[‘user_id’] ; to work in my class, since I need it in all functions within that class, i.e.
WHERE operator_id = :operator
Where : operator should represent $_SESSION[‘user_id’]