Component

Module

SQL Debugging

To show SQL error while developing

Directory

File Folder Link
Framework.php \\SYNAS\Allan\DOCUMENTATION\Component\SQL Debugging\rckuc\wolf
backend.php \\SYNAS\Allan\DOCUMENTATION\Component\SQL Debugging\rckuc\wolf\app\layouts

 

Step 1

Update: Framework.php
In class Record ->public function save() Insert this code for Insert & Update part:

if ($return === false) { $errorInfo = self::$__CONN__->errorInfo(); Flash::set('sql_error', __(json_encode($errorInfo))); Flash::set('sql_query', __($sql)); }
if ($return === false) {
    $errorInfo = self::$__CONN__->errorInfo();
    Flash::set('sql_error', __(json_encode($errorInfo)));
    Flash::set('sql_query', __($sql));
}

Step 2

Update: backend.php
Add this logic to check and display sql error

<?php if (Flash::get('sql_error') !== null && ModuleSetting::findSetting('all', 'Enable SQL Debugging') == "1"): ?> <?php $sql_full = Flash::get('sql_query'); if (mb_strlen($sql_full) > 100) { $sql_short = mb_strimwidth($sql_full, 0, 100, '... (view console to get full sql query)'); $show_console = true; } else { $sql_short = $sql_full; $show_console = false; } ?> <script> <?php if ($show_console): ?> console.log("FULL SQL QUERY:", `<?php echo htmlspecialchars(addslashes($sql_full)); ?>`); <?php endif; ?> Swal.fire({ icon: 'error', title: 'Database Error', html: `<div style="text-align: left"> <strong>Error Message:</strong><br> <?php echo htmlspecialchars(Flash::get('sql_error'), ENT_QUOTES); ?> <br><br> <strong>SQL Query:</strong><br> <?php echo htmlspecialchars($sql_short, ENT_QUOTES); ?> </div>` }); </script> <?php endif; ?>
<?php if (Flash::get('sql_error') !== null && ModuleSetting::findSetting('all', 'Enable SQL Debugging') == "1"): ?>
    <?php
        $sql_full = Flash::get('sql_query');

        if (mb_strlen($sql_full) > 100) {
            $sql_short = mb_strimwidth($sql_full, 0, 100, '... (view console to get full sql query)');
            $show_console = true;
        } else {
            $sql_short = $sql_full;
            $show_console = false;
        }
    ?>
    <script>
        <?php if ($show_console): ?>
            console.log("FULL SQL QUERY:", `<?php echo htmlspecialchars(addslashes($sql_full)); ?>`);
        <?php endif; ?>

        Swal.fire({
            icon: 'error',
            title: 'Database Error',
            html: `<div style="text-align: left">
                        <strong>Error Message:</strong><br>
                        <?php echo htmlspecialchars(Flash::get('sql_error'), ENT_QUOTES); ?>
                        <br><br>
                        <strong>SQL Query:</strong><br>
                        <?php echo htmlspecialchars($sql_short, ENT_QUOTES); ?>
                    </div>`
        });
    </script>
<?php endif; ?>

Step 3

Insert SQL Record into wolf_module_setting:

INSERT INTO `wolf_module_setting` (`id`, `module`, `name`, `value_type`, `value`, `created_on`, `updated_on`, `created_by_id`, `updated_by_id`) VALUES (NULL, 'all', 'Enable SQL Debugging', 'checkbox', '1', NOW(), NULL, NULL, NULL);
INSERT INTO `wolf_module_setting` (`id`, `module`, `name`, `value_type`, `value`, `created_on`, `updated_on`, `created_by_id`, `updated_by_id`) VALUES (NULL, 'all', 'Enable SQL Debugging', 'checkbox', '1', NOW(), NULL, NULL, NULL);
Code Copied To Clipboard!