4

This is the code for printing last excuted query in codeigniter is

print_r($this->db->last_query());

As like what is the code for printing last executed query in PHP?

Kichu
  • 3,284
  • 15
  • 69
  • 135

2 Answers2

1

You could do something like this to get last executed query:

$mysql_last_query = ""; //this var contains the last mysql query
function query($sql, $args) { #sintax: query("query", "arg1", "arg2", "argn"); EXAMPLE: query("select * from %s", $_GET["table"]);
    global $mysql_last_query;
    $args = func_get_args();
    if(($num = func_num_args()) > 1)
        $sql = call_user_func_array("sprintf", $args);
    $mysql_last_query = $sql;
    return mysql_query($sql);
}
function select($table, $what = "*", $where = "", $limit = ""){ #this function is a "shortcut" for mysql select EXAMPLE: select("blah"); //SELECT * FROM blah and so on
    return mysql_query("SELECT ".$what." FROM ".$table.($where == "" ? "":(" WHERE ".$where)).($limit == "" ? "":(" LIMIT ".$limit)));
}

Hope it helps

Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
1

This is a seemingly common question. The intended way to do it seems to depend on logging: How to show the last queries executed on MySQL?

Community
  • 1
  • 1
landons
  • 9,502
  • 3
  • 33
  • 46