• I am writing a plugin for someone (already finished really), but they want to be able to see if the DB table for the information exists within the settings page that has been made.

    I can’t for the life of me work this one out, and thought I should ask instead of spending another day blindly stabbing in the dark and hoping I get something.

    The first issue I came across was when trying to build a multi-string echo statement:

    echo "<h3>Database Status:</h3>" + "<h3 style='color: green;'> Online</h3>";

    Upon doing this, in the settings page the entire line is replaced with a 0.

    Not sure why this is, I think I have laid it out correctly, but the problem is I am a C# developer and am only just moving into PHP and the syntax seems very different ??

    What I think I want to do is split the echo so there is an if statement checking if the database exists, then placing in either “Online” in green if it is, and “Offline” in red if it isn’t, but nothing I try or find online seems to help with checking the table.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi,

    You can use the following condition to check if table exists or not and display appropriate message.

    global $wpdb;
    $table_name = $wpdb->prefix.'TABLE-NAME-HERE';
    if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
         echo "<h3>Database Status:</h3>"."<h3 style='color: green;'> Online</h3>";
    }
    else{
    	echo "<h3>Database Status:</h3>"."<h3 style='color: red;'> Offline</h3>";
    }

    Please let mwe know if it works for you.

    Thanks

    Thread Starter kjgbriggs

    (@kjgbriggs)

    I changed it slightly to allow for in-line text, and changed this to the table name, but it is registering it as “offline” constantly.

    I know it exists as I am looking at it in MySql at the moment.

    global $wpdb;
    	$table_name = $wpdb->prefix.'ftbstatistics';
    	if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
         		echo "<h3>Database Status: <span style='color: green;'>Online</span></h3>";
    	}
    	else{
    		echo "<h3>Database Status: <span style='color: red;'>Offline</span></h3>";
    	}

    Hi,

    Sorry, actually the offline and online were placed under wrong conditions.

    Please use the below code it will work this time.

    global $wpdb;
    	$table_name = $wpdb->prefix.'ftbstatistics';
    	if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
                    //Table does not exits
         		echo "<h3>Database Status: <span style='color: red;'>Offline</span></h3>";
    	}
    	else{
                    //Table exits
                    echo "<h3>Database Status: <span style='color: green;'>Online</span></h3>";
    	}

    Thanks

    Thread Starter kjgbriggs

    (@kjgbriggs)

    Ah, that worked.
    Thank you very much for this, been bugging me for the last day and a bit.

    Your Welcome. ??

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Checking if Table Exists & Printing Information’ is closed to new replies.