Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home PHP CodeIgniter Save Or Log All MySQL Queries

CodeIgniter Save Or Log All MySQL Queries

While developing CodeIgniter application, we may need to save or log all SQL queries in order to debug the code or just want to know exactly what happens with MySQL database.

Save Or Log All SQL Queries in CodeIgniter

Below are 3 simple steps need to be implemented before we can record MySQL queries.

1. Enable System Hooks feature – set enable_hooks to TRUE in /application/config/config.php file

/application/config/config.php

1
2
3
4
5
6
7
8
9
10
/*
|--------------------------------------------------------------------------
| Enable/Disable System Hooks
|--------------------------------------------------------------------------
|
| If you would like to use the 'hooks' feature you must enable it by
| setting this variable to TRUE (boolean).  See the user guide for details.
|
*/
$config['enable_hooks'] = TRUE;

/* |-------------------------------------------------------------------------- | Enable/Disable System Hooks |-------------------------------------------------------------------------- | | If you would like to use the 'hooks' feature you must enable it by | setting this variable to TRUE (boolean). See the user guide for details. | */ $config['enable_hooks'] = TRUE;

2. Define the ‘Hook’ – open the /application/config/hooks.php and add the
following PHP code:

/application/config/hooks.php

1
2
3
4
5
6
$hook['display_override'][] = array(
        'class' => '',
        'function' => 'log_queries',
        'filename' => 'log_queries.php',
        'filepath' => 'hooks'
);

$hook['display_override'][] = array( 'class' => '', 'function' => 'log_queries', 'filename' => 'log_queries.php', 'filepath' => 'hooks' );

3. Implement the hook which was defined in step #2 – create a new file named log_queries.php in /application/hooks/ and add following PHP code:

/application/hooks/log_queries.php

1
2
3
4
5
6
7
8
9
10
11
12
<?php
function log_queries() 
{
	$CI =& get_instance();
	$times = $CI->db->query_times;
	foreach ($CI->db->queries as $key=>$query) {
		echo "Query: ". $query." | ".$times[$key] . "<br>";
	}
 
	$CI->output->_display();
}
?>

<?php function log_queries() { $CI =& get_instance(); $times = $CI->db->query_times; foreach ($CI->db->queries as $key=>$query) { echo "Query: ". $query." | ".$times[$key] . "<br>"; } $CI->output->_display(); } ?>

In the log_queries function above, we display all SQL queries on the browser. If you like to write/save them to a log file or even store them all in database, simple revise the foreach loop.

Oct 31, 2014Hoan Huynh
Get Familiar With Microsoft Dynamics CRM Development Customization LevelsSystem.Security.SecurityException: That assembly does not allow partially trusted callers
You Might Also Like:
  • CodeIgniter Send Email Via SMTP Charset UTF-8
  • Configure To Run Codeigniter In Sub Folder Or Sub Directory
  • CodeIgniter Upload And ReSize Image Maintain Ratio
  • Codeigniter Pagination Show Total Pages
  • Free Mysql Profiler similar to Microsoft SQL Server Profiler – LogMonitor
  • PHP Load Facebook Albums And Save To MySQL Database
  • PHP Save String Content To File
  • How To Save PHP Error Log To File In IIS
  • C# Save Web Page URL To Image
  • How To Tune And Optimize MySQL Performance With MySQLTuner
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

7 years ago PHPCodeigniter, CodeIgniter Hooks, display_override, enable_hooks, get_instance, query_times2,975
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
24,558 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
21,892 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
17,746 views
JQuery Allow only numeric characters or only alphabet characters in textbox
15,071 views
C# Read Json From URL And Parse/Deserialize Json
11,805 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Things to Learn about Installingderm Loan Type S
  • Online Photo Editor – Free Photoediting Software
  • A Guide to Finding the Best Paper Sellers
  • Photoediting in Home Isn’t Hard to Do!

  • Free Photo Editor Online
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (27)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (647)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development