Skip to content
Advertisement

Proper way for WHERE clause with PHP and MySQL

I’m creating a plugin, and I have one problem.

I’m trying to loop through the database table and to show only the last values from the column.

For example:

id | plugin_name                    | plugin_path                   | plugin_time_delay |
-----------------------------------------------------------------------------------------
1  | Elementor                      | elementor/elementor.php       | 5                 |
-----------------------------------------------------------------------------------------
2  | Health Check & Troubleshooting | health-check/health-check.php | 3                 |
-----------------------------------------------------------------------------------------
3  | Elementor                      | elementor/elementor.php       | 7                 |
-----------------------------------------------------------------------------------------

What I need is to show only the last value for example for Elementor value.

This is what I have tried so far:

$all_plugins = get_plugins();

foreach ( $allPlugins as $key => $allPlugin ) {
    $queryPlugins = $allPlugin['Name'];

    $selectPluginSeconds = $wpdb->get_results( 'SELECT * FROM '.$wpdb->prefix.'wdl WHERE plugin_name=".$queryPlugins." ORDER BY id DESC LIMIT 1' );

    foreach ( $selectPluginSeconds as $select_plugin_second ) {
        if ($allPlugin['Name'] == $select_plugin_second->plugin_name) { ?>
            <input type="text" style="width: 200px" value="<?= $select_plugin_second->plugin_time_delay . __( ' seconds.', 'wdl') ?>" disabled>
        <?php }
    }
}

In this way I don’t get any values, ( see the image ) only an empty field. But as soon as I change $selectPluginSeconds query on this way ( with static value ) then I get only that static value ( see the image ) :

$selectPluginSeconds = $wpdb->get_results( 'SELECT * FROM '.$wpdb->prefix.'wdl WHERE plugin_name="Elementor" ORDER BY id DESC LIMIT 1' );

Advertisement

Answer

The concatenation was a little mixed up, this should work

$selectPluginSeconds = $wpdb->get_results( 
        "SELECT * 
        FROM {$wpdb->prefix}wdl 
        WHERE plugin_name='$queryPlugins' 
        ORDER BY id DESC 
        LIMIT 1" );
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement