Last executed query in MySQL
Q: Is there any way to list last executed queries in MySql?
A:
Ofcourse, MySQL offers various types of logs to inspect and improve query performance. We can retrive last executed queries once MySQL general query log enabled.
You've two ways to enable mysql general log
Output mysql queries to table:
You can enable MySQL to output query log to a table if your mysql version >= 5.1. To enable query log set following options after creating a database connection
After this all your queries logged into mysql.general_log table, have a look on it.
Output mysql queries to file:
Execute these queries to log queries into a flat file.
Note: Log file must be created and set appropriate permission to enable mysql to read/write into specified file. If things not going in your way, use mysql_error() to find out whats going on...
Example:
( or )
A:
Ofcourse, MySQL offers various types of logs to inspect and improve query performance. We can retrive last executed queries once MySQL general query log enabled.
The general query log is a general record of what 'mysqld' is doing
You've two ways to enable mysql general log
- Output to table
- Output to flat file
Output mysql queries to table:
You can enable MySQL to output query log to a table if your mysql version >= 5.1. To enable query log set following options after creating a database connection
mysql_query('SET GLOBAL log_output = "TABLE"';
mysql_query('SET GLOBAL general_log = "ON"') ;
After this all your queries logged into mysql.general_log table, have a look on it.
Output mysql queries to file:
Execute these queries to log queries into a flat file.
mysql_query('SET GLOBAL log_output = "FILE"');
mysql_query('SET GLOBAL general_log = "ON"') ;
mysql_query('SET GLOBAL general_log_file = "/path/of/the/file/query.log"');
Note: Log file must be created and set appropriate permission to enable mysql to read/write into specified file. If things not going in your way, use mysql_error() to find out whats going on...
Example:
<?php
$ln=mysql_connect('localhost','root','') or die('Connection Failed');
mysql_select_db('xlogin_new',$ln) or die('Unable to select database');
mysql_query('SET GLOBAL log_output = "TABLE"',$ln);
mysql_query('SET GLOBAL general_log = "ON"',$ln) ;
?>
( or )
<?php
mysql_query('SET GLOBAL log_output = "FILE"');
mysql_query('SET GLOBAL general_log = "ON"') ;
mysql_query('SET GLOBAL general_log_file = "/path/of/the/file/query.log"');
?>
Comments (0)