The code is wrong at a first look, there is an unfinished $". If I want to log my sql exec - I use sqlite database - I copy the database to my windows PC and use the sqlitestudio program do debug the sql statements. I inspect all variables value involved in statements and try execeute in sqlitestudio.