943,947 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 2506
  • PHP RSS
Oct 24th, 2009
0

PDO Number of Total Queries per Page

Expand Post »
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.

PHP Syntax (Toggle Plain Text)
  1. class myPDO extends PDO{
  2. var $myOwnCount =0;
  3. public function __construct($location,$user,$pass)
  4. {
  5. parent::__construct($location,$user,$pass);
  6. }
  7. public function exec($q){
  8. $this->myOwnCount ++;
  9. return parent::exec($s);
  10. }
  11. public function query($q){
  12. $this->myOwnCount ++;
  13. return parent::query($s);
  14. }
  15. }
Gave those Errors:
PHP Syntax (Toggle Plain Text)
  1. Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\...\functions.php on line 52
  2. 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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
opatut is offline Offline
5 posts
since Oct 2009
Oct 24th, 2009
0
Re: PDO Number of Total Queries per Page
Quote ...
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)


Quote ...
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);
Reputation Points: 7
Solved Threads: 14
Junior Poster
smartness is offline Offline
103 posts
since Aug 2007
Oct 24th, 2009
0
Re: PDO Number of Total Queries per Page
I Create the Object like this:
PHP Syntax (Toggle Plain Text)
  1. $dataBase = new myPDO($location, $user, $pass);
If I use PDO instead, it works:
PHP Syntax (Toggle Plain Text)
  1. $dataBase = new PDO($location, $user, $pass);

Functions.php around line 52:
php Syntax (Toggle Plain Text)
  1. $sql = $database->query("SELECT id,title FROM general");
  2. foreach ($sql as $general)
  3. $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...
Last edited by opatut; Oct 24th, 2009 at 3:11 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
opatut is offline Offline
5 posts
since Oct 2009
Oct 24th, 2009
0
Re: PDO Number of Total Queries per Page
Quote ...
$sql = $database->query("SELECT id,title FROM general");
foreach ($sql as $general)
$generalData[$general["id"]] = $g["title"];
Edited, removed!

EDIT: Glad you solved it!
Last edited by smartness; Oct 24th, 2009 at 3:29 pm.
Reputation Points: 7
Solved Threads: 14
Junior Poster
smartness is offline Offline
103 posts
since Aug 2007
Oct 24th, 2009
0
Re: PDO Number of Total Queries per Page
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
Last edited by peter_budo; Oct 25th, 2009 at 6:59 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
opatut is offline Offline
5 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: block js injections through php
Next Thread in PHP Forum Timeline: Really stupid





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC