• Resolved hamjaiu

    (@hamjaiu)


    This is my code my problem is when i check this code in wordpress plugin checker then show me the error  
    1. WARNING WordPress.DB.DirectDatabaseQuery.DirectQuery Use of a direct database call is discouraged.
    2. WARNING WordPress.DB.DirectDatabaseQuery.NoCaching Direct database call without caching detected. Consider using wp_cache_get() / wp_cache_set() or wp_cache_delete().

    How can i solved this problem ?

    <?php
    /********************************
    * This file is for creating a database table for Leaflet.js map
    ****/
    // Create database tables
    function ikr_js_db_connection() {
    global $wpdb;

    // Table prefixes
    $table_name_1 = $wpdb->prefix . 'ikr_leaflet_js_db';
    $table_name_2 = $wpdb->prefix . 'ikr_default_setting';
    $charset_collate = $wpdb->get_charset_collate();

    // Create table 1 if it doesn't exist
    // changed from General to Database
    $table_exists_1 = $wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE %s", $table_name_1 ) );
    if ( $table_exists_1 != $table_name_1 ) {

    $sql = "CREATE TABLE $table_name_1 (
    id INT(10) NOT NULL AUTO_INCREMENT,
    lat VARCHAR(100) NOT NULL,
    lng VARCHAR(100) NOT NULL,
    address VARCHAR(100) NOT NULL,
    phone VARCHAR(100) NOT NULL,
    email VARCHAR(100) NOT NULL,
    urls VARCHAR(1000) NOT NULL,
    marker_id VARCHAR(100) NOT NULL,
    PRIMARY KEY (id)
    ) $charset_collate;";
    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    dbDelta( $sql );

    // Insert default data using prepared statements
    $wpdb->insert(
    $table_name_1,
    array(
    'lat' => 23.8536047088421,
    'lng' => 89.24606323242189,
    'address' => '2',
    'phone' => '100',
    'email' => '300',
    'urls' => 'https://hamjaiu.com',
    'marker_id' => '88759345',
    )
    );
    }

    // Create table 2 if it doesn't exist
    $table_exists_1 = $wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE %s", $table_name_2 ) );
    if ( $table_exists_1 != $table_name_2 ) {

    $sql = "CREATE TABLE $table_name_2 (
    id INT(10) NOT NULL AUTO_INCREMENT,
    Latitude FLOAT NOT NULL DEFAULT 23.8536047088421,
    Longitude FLOAT NOT NULL DEFAULT 89.24606323242189,
    zoom FLOAT NOT NULL DEFAULT 12,
    width FLOAT NOT NULL DEFAULT 50,
    height FLOAT NOT NULL DEFAULT 50,
    zoom_option VARCHAR(100) DEFAULT NULL,
    PRIMARY KEY (id)
    ) $charset_collate;";
    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    dbDelta( $sql );

    // Insert default data using prepared statements
    $wpdb->insert(
    $table_name_2,
    array(
    'Latitude' => 23.8536047088421,
    'Longitude' => 89.24606323242189,
    'zoom' => 2,
    'width' => 100,
    'height' => 300,
    'zoom_option' => 'auto_zoom',
    )
    );
    }
    }

    // Call the function to create/update tables
    ikr_js_db_connection();
    ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • WordPress prefers that use of existing API instead of direct database queries. You can rewrite the code as below. I have added caching code for db exists and added phpcs:ignore for DirectQuery error.

    // Create database tables
    function ikr_js_db_connection() {
    global $wpdb;

    // Table prefixes
    $table_name_1 = $wpdb->prefix . 'ikr_leaflet_js_db';
    $table_name_2 = $wpdb->prefix . 'ikr_default_setting';
    $charset_collate = $wpdb->get_charset_collate();

    // Define a cache key for this specific check
    $cache_key = 'ikr_leaflet_js_db_exists';

    // Try to get the cached result first
    $table_exists_1 = wp_cache_get( $cache_key );

    if ( false === $table_exists_1 ) {

    // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
    $table_exists_1 = $wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE %s", $table_name_1 ) );

    // Cache the result to avoid future database hits
    wp_cache_set( $cache_key, $table_exists_1 );
    }

    // Create table 1 if it doesn't exist
    if ( $table_exists_1 ) {

    $sql = "CREATE TABLE $table_name_1 (
    id INT(10) NOT NULL AUTO_INCREMENT,
    lat VARCHAR(100) NOT NULL,
    lng VARCHAR(100) NOT NULL,
    address VARCHAR(100) NOT NULL,
    phone VARCHAR(100) NOT NULL,
    email VARCHAR(100) NOT NULL,
    urls VARCHAR(1000) NOT NULL,
    marker_id VARCHAR(100) NOT NULL,
    PRIMARY KEY (id)
    ) $charset_collate;";
    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    dbDelta( $sql );

    // Insert default data using prepared statements
    // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
    $wpdb->insert(
    $table_name_1,
    array(
    'lat' => 23.8536047088421,
    'lng' => 89.24606323242189,
    'address' => '2',
    'phone' => '100',
    'email' => '300',
    'urls' => 'https://hamjaiu.com',
    'marker_id' => '88759345',
    )
    );
    }

    // Do same for table 2
    }

    // Call the function to create/update tables
    ikr_js_db_connection();
    Thread Starter hamjaiu

    (@hamjaiu)

    Thank you Kprajapati for your reply. This code correctly removes the error.

    • This reply was modified 1 month ago by hamjaiu.
Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.