SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Enthusiast the-webber's Avatar
    Join Date
    Mar 2009
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Output mysql's EXPLAIN via php

    anyone know if there's a way (and if so how) to output the result of mysql EXPLAIN via PHP. I just want to be able see what MySql returns from a query like "EXPLAIN SELECT stuff from my_table";

    I know I can do this directly in mysql, but it's a pain in the butt and would like to be able to call it directly from a php script

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    $dbg=1;  // careful !

    // say you have a query in a page, assign it to a variable rather than build it up

    $id = (int)$_GET['id'] ;

    $sql "select things from table where id = $id";

    // now do your normal query 
    mysql_fetch_*($sql);

    if( 
    $dbg ) {
    // now do your extra query here

    mysql_fetch_* ("explain " $sql ) ;

    // echo results


    Either that or get mysql to log your queries and pick them up from that.

    If you are using OOP it is quite easy to build an extra method somewhere that does the same thing.

    $db->explain( $sql );

    where 'explain' maybe does the query and then sends it to a log.

  3. #3
    SitePoint Enthusiast the-webber's Avatar
    Join Date
    Mar 2009
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks! worked great!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •