Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • logging SQL queries

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 105
    Comment on it

    AJAX request runs a RAW parameterized Postgres SQL Query after receiving request

    Suppose we have a situation like that

    Here's my situation

    in my php view file, I make AJAX request to the server

    The AJAX request is received and runs a RAW parameterized Postgres SQL query (e.g.

    DB::select('select * from my_table where id=?', array(1))
    

    Then to solve this problem we will use such logging sql query.

    if (Config::get('database.log', false))
    {           
        Event::listen('illuminate.query', function($query, $bindings, $time, $name)
        {
            $data = compact('bindings', 'time', 'name');
    
            // Format binding data for sql insertion
            foreach ($bindings as $i => $binding)
            {   
                if ($binding instanceof \DateTime)
                {   
                    $bindings[$i] = $binding->format('\'Y-m-d H:i:s\'');
                }
                else if (is_string($binding))
                {   
                    $bindings[$i] = "'$binding'";
                }   
            }       
    
            // Insert bindings into query
            $query = str_replace(array('%', '?'), array('%%', '%s'), $query);
            $query = vsprintf($query, $bindings); 
    
            Log::info($query, $data);
        });
    }
    

    By this way we can deal with sql logging query.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: