Search code examples
phpwordpressadminer

Managing Wordpress Database (Adminer)


Im new to wordpress and having difficulties in managing the database. I have installed "Adminer" plugin to manage the database. I already created a new table called "usersupp_admin". Now I also created a custom template that will connect to this database using PDO statements, the problem now is where can I get the host, dbname, user, and password? Here's the codes:

<?php

 $host   = "";
 $dbname = "";
 $user   = "";
 $pass   = "";

 $conn = new PDO("mysql:host=$host;dbname=$dbname",$user,$pass);
 $sql = "SELECT * FROM usersupp_admin";
 $q   = $conn->query($sql) or die("failed!");
 $result = $q->fetch(PDO::FETCH_ASSOC);
 $user_db=$result['username'];
 echo $user_db;
 ?>

Any help would be very much appreciated.


Solution

  • Hey You don't require to create connection once again if you do it once in theme.

    Just simply have to add following code in your external file.

    /* Short and sweet */
    define('WP_USE_THEMES', false);
    require('../../../wp-blog-header.php');
    ?>
    

    wp-blog-header.php is located at root of your theme. This file you have to include, After including you can access wordpress in any external template.

    Reference from

    You don't require that any type of connection, if you include this file once. for example :

    define('WP_USE_THEMES', false);
    require('./wp-blog-header.php');
    header('HTTP/1.1 200 OK');
    global $wpdb;
    
    $rs = $wpdb->get_results("SELECT * FROM usersupp_admin", ARRAY_A); 
    
    if (count($rs) > 0) {
        echo $username =  $rs['username'];
    }