how can we get details from another database inside a plugin in wordpress

114 Views Asked by At

I created a plugin in wordpress to display earnings by using a shortcode.The details to display while using shortcode are stored in another database.I used direct database connection in plugin to fetch details from the that database.I used the following code

function earnings_shortcode($atts, $content, $tag)
     {  //echo $atts[0];echo '<br>';
         $str=base64_encode(1);
         base64_decode($str);
         $length = 4;
         $res = trim(preg_replace("/[^0-9]/", "", $atts[0]));
$mydb1 = new wpdb('root','','db_test','localhost');
$rows = $mydb1->get_row("SELECT total,paydate FROM `tbl_shotcode` WHERE userid = $res", ARRAY_A);
echo "Payout on -" .$rows['paydate']; echo '<br/>';
echo "Total for next Pay Period:-" .$rows['total'];
}

Is there any better option to access another database inside a plugin with out hard coding the username and password.please suggest a solution.

1

There are 1 best solutions below

0
On

No, you have to access the database and be authenticated to do queries. You can't get into a properly configured database without login in.

You can, however, make your $mydb1 variable global and define it at the top of your file (or in your constructor class) to be accessed by all your functions. I recommend putting it in a class to manage it more easily.