PDO Number of Total Queries per Page

Thread Solved

Join Date: Oct 2009
Posts: 5
Reputation: opatut is an unknown quantity at this point 
Solved Threads: 0
opatut opatut is offline Offline
Newbie Poster

PDO Number of Total Queries per Page

 
0
  #1
32 Days Ago
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.

  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:
  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 72
Reputation: smartness is an unknown quantity at this point 
Solved Threads: 10
smartness's Avatar
smartness smartness is offline Offline
Junior Poster in Training
 
0
  #2
32 Days Ago
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);
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 5
Reputation: opatut is an unknown quantity at this point 
Solved Threads: 0
opatut opatut is offline Offline
Newbie Poster
 
0
  #3
32 Days Ago
I Create the Object like this:
  1. $dataBase = new myPDO($location, $user, $pass);
If I use PDO instead, it works:
  1. $dataBase = new PDO($location, $user, $pass);

Functions.php around line 52:
  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; 32 Days Ago at 3:11 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 72
Reputation: smartness is an unknown quantity at this point 
Solved Threads: 10
smartness's Avatar
smartness smartness is offline Offline
Junior Poster in Training
 
0
  #4
32 Days Ago
$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; 32 Days Ago at 3:29 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 5
Reputation: opatut is an unknown quantity at this point 
Solved Threads: 0
opatut opatut is offline Offline
Newbie Poster
 
0
  #5
32 Days Ago
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; 31 Days Ago at 6:59 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
Reply With Quote Quick reply to this message  
Reply

Tags
number, php, query, sql

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC