Hi Community!

I have already searched the whole web about this Problem, and I didn't find anything. Maybe you can help...

I want to show the number of all SQL queries I executed to load a web page. It is a very complex system I programmed, and I don't want to count them manually. I use PDO for this, so I can't easily write a function count_query($sql) which increases a counter and return query($sql) I have already tried to write my an "myPDO"-class, extended from PDO, to do this job, but it didn't work.

class myPDO extends PDO{
    var $myOwnCount =0;
    public function __construct($location,$user,$pass)
    {
        parent::__construct($location,$user,$pass);
    } 
    public function exec($q){
        $this->myOwnCount ++;
        return parent::exec($s);
    }
    public function query($q){
        $this->myOwnCount ++;
        return parent::query($s);
    }
}

Gave those Errors:

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\...\functions.php on line 52
Fatal error: Call to a member function fetch() on a non-object in C:\xampp\htdocs\...\index.php on line 69

Thanks for help

Paul

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\...\functions.php on line 52

What is the Code in functions.php on line 52? (the whole function if possible)

Fatal error: Call to a member function fetch() on a non-object in C:\xampp\htdocs\...\index.php on line 69

I think this is caused because you didn't create a instance of the class

$class = new YourClassName();

Or declare fetch as a static public function(), but when you access it you call it like

YourClassName::fetch($arguments_here_if_any);

I Create the Object like this:

$dataBase = new myPDO($location, $user, $pass);

If I use PDO instead, it works:

$dataBase = new PDO($location, $user, $pass);

Functions.php around line 52:

$sql = $database->query("SELECT id,title FROM general");
foreach ($sql as $general)
    $generalData[$general["id"]] = $g["title"];

The Problem is that you can use the PDOStatement in foreach like an Array. My class doesn't support this, but it should because it is extended from PDO...

$sql = $database->query("SELECT id,title FROM general");
foreach ($sql as $general)
$generalData[$general["id"]] = $g["title"];

Edited, removed!

EDIT: Glad you solved it!

I FOUND IT!!!

public function query($q){
     $this->myOwnCount ++;
     return parent::query($s);
}

This cant work! Thanks for helping, but it was - of course - a little mistake.

Paul

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.