debug SQL Statement w. PHP lib

debug SQL Statement w. PHP lib

dg_datatablesdg_datatables Posts: 53Questions: 10Answers: 1

Hello,

iam trying to debug a SQL statement, when i use the PHP libs from DT Editor.
The debug function works fine, when is use the Editor Class. But sometimes is need the Database/Query Class.
https://editor.datatables.net/docs/1.7.3/php/class-DataTables.Database.html#_debug

$data = $mydb
->query( 'select' )
->table( 'important_table' )
->get( array( "important_column" ) )
->where ( "id", 5, "=" )
->exec()->fetchAll();

When i add ->debug(true) before ->query (like the "Editor" way)
Fatal error: Function name must be a string in [...]/libs/datatables/php/Database/Database.php on line 422

It won't work with debugInfo() too. I don't know how to receive the information.

Thanks alot in advance.

Replies

  • allanallan Posts: 63,689Questions: 1Answers: 10,500 Site admin

    The Database class also has debug() methods available. Try:

    $mydb->debug( true );
    
    $data = $mydb->query....;
    
    $mydb->debugInfo();
    

    Note that debugInfo isn't documented there - its an internal function that I didn't really expect to be used externally, but it is public.

    Allan

  • dg_datatablesdg_datatables Posts: 53Questions: 10Answers: 1

    Thanks for the reply. I forgot to mention, that i use oracle as db.
    When i try your example it also tells me:

    Fatal error: Function name must be a string in .....

    So when you set ->debug(true), the internal variable ->_debugCallback will be set to true. When you call ->debugInfo() a callback function named "true"(from ->_debugCallback) fails.

    PHP debug:

    public function debug ( $set=null )
    {
    if ( $set === null ) {
    return $this->_debugCallback ? true : false;
    }
    else if ( $set === false ) {
    $this->_debugCallback = null;
    }
    else {
    $this->_debugCallback = $set;
    }

      return $this;
    

    }

    PHP debugInfo:

    $callback = $this->_debugCallback;

    if ( $callback ) {
    $callback( array(
    "query" => $query,
    "bindings" => $bindings
    ) );
    }

    return $this;

    If you try a string instead of bool(true) in ->debug('veryImportantString') it says:

    Fatal error: Call to undefined function veryImportantString() in ....

  • allanallan Posts: 63,689Questions: 1Answers: 10,500 Site admin

    Can you show me the code you are using please?

    Allan

  • dg_datatablesdg_datatables Posts: 53Questions: 10Answers: 1
    edited April 2018

    I've created this simple example, which leads to the error. Iam using version 1.7.3.

    <?php
    
    include_once "inc/datatables/php/DataTables.php";
    
    $db->debug( true );
    
    $data = $db
            ->query( "select" )
            ->table( "important_table" )
            ->get( array( "important_column" ) )
            ->where ( "id", 5, "=" )
            ->exec()->fetchAll();
    
    $db->debugInfo();
    ?>
    
  • allanallan Posts: 63,689Questions: 1Answers: 10,500 Site admin

    Sorry - I forgot I refactored that bit not long ago!

    Use this:

    $debug = [];
    $db->debug( function ( $mess ) use ( &$debug ) {
        $debug[] = $mess;
    } );
    

    Then all the debug statements will be written to $debug.

    Allan

  • dg_datatablesdg_datatables Posts: 53Questions: 10Answers: 1

    Ok that's the solution i was looking for. Thank you very much, great help as always.
    Here is a working example:

    <?php
    include_once "datatables/php/DataTables.php";
    
    $debug = [];
    $db->debug( function ( $mess ) use ( &$debug ) {
        $debug[] = $mess;
    } );
    
    $data = $db
            ->query( "select" )
            ->table( "important_table" )
            ->get( array( "important_column" ) )
            ->where ( "id", 5, "=" )
            ->exec()->fetchAll();
    
    echo "<pre>";
    print_r($debug);
    echo "</pre>";
    

    With this output:

     Array
     (
         [0] => Array
             (
                 [query] => SELECT  IMPORTANT_COLUMN as "IMPORTANT_COLUMN" FROM  IMPORTANT_TABLE WHERE ID = :where_0 
                 [bindings] => Array
                     (
                         [0] => Array
                             (
                                 [name] => :where_0
                                 [value] => 5
                                 [type] => 
                             )
    
                     )
    
             )
    
     )
    
This discussion has been closed.