• Hi. I am trying to create a WordPress plugin that connects to a Planet Scale database, pulls data from a table and implements a short code to display the data in posts across the website.

    I have tested the connection thoroughly using php and have also managed to retrieve data (the required SSL, username and password are all correct).

    However, when I try to replicate the same functionality in WordPress, I get a host of warnings that indicate that I cannot connect to the database. The warnings are given below:

    Warning: failed loading cafile stream: `cacert.pem' in /home/u123878211/domains/abc.xyz/public_html/mlsdev/wp-content/plugins/aislingcs-mls/db.php on line 13
    
    Warning: mysqli::real_connect(): Cannot connect to MySQL by using SSL in /home/u123878211/domains/abc.xyz/public_html/mlsdev/wp-content/plugins/aislingcs-mls/db.php on line 13
    
    Warning: mysqli::real_connect(): [2002] (trying to connect via (null)) in /home/u123878211/domains/abc.xyz/public_html/mlsdev/wp-content/plugins/aislingcs-mls/db.php on line 13
    
    Warning: mysqli::real_connect(): (HY000/2002): in /home/u123878211/domains/abc.xyz/public_html/mlsdev/wp-content/plugins/aislingcs-mls/db.php on line 13
    Successfully connected to the database

    In terms of the SSL certificate errors, I have doubled checked that the pem file is in the correct location and, I have also set “chmod 644” using SSH to verify the correct permissions.

    **Although it shows a message that it is connected to the DB, I am unable to run queries and, the warnings indicate that it is not.
    The following are the contents of the main plugin file and, below that is the content that contains the logic to connect to the DB.

    <?php
    /*
     * Plugin Name:       Aislingcs MLS
    */
    
    
    include 'db.php';
    
    register_activation_hook(__FILE__, 'db_connect');
    register_deactivation_hook(__FILE__, 'db_disconnect');
    
    // Shortcode creation
    function db_display_shortcode($atts)
    {
        if (!$mysqli) {
            return 'Error: Remote database connection not available.';
        }
    
        $query = 'SELECT * FROM test_table';
        $result = mysqli_query($mysqli, $query);
        if (!$result) {
            return 'Error: Failed to retrieve data from the remote database.';
        }
    
        $output = '<ul>';
        while ($row = mysqli_fetch_assoc($result)) {
            $output .= '<li>' . $row['column_name'] . '</li>';
        }
        $output .= '</ul>';
    
        mysqli_free_result($result);
    
        return $output;
    }
    add_shortcode('db_display', 'db_display_shortcode');
    
    <?php
    include 'vendor/autoload.php';
    include 'db-details.php';
    
    $hostname = $HOST;
    $dbName = $DATABASE;
    $username = $USER_NAME;
    $password = $PASSWORD;
    $ssl = $SSL_CERT;
    
    $mysqli = mysqli_init();
    $mysqli->ssl_set(NULL, NULL, $ssl, NULL, NULL);
    $mysqli->real_connect($hostname, $username, $password, $dbName);
    
    if (!$mysqli) {
        echo "Not connected to the database";
    } else {
        echo "Successfully connected to the database";
    }
    
    function db_connect()
    {
        global $mysqli;
        update_option('remote_db_connection', $mysqli);
    }
    
    function db_disconnect()
    {
        delete_option('remote_db_connection');
    }
    ?>
    
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Your $mysqli connection object is out of scope from within the shortcode callback. You need to either declare it global or create the connection locally within the shortcode callback.

Viewing 1 replies (of 1 total)
  • The topic ‘Connect Database (PlanetScale) to WordPress’ is closed to new replies.