0

I developed a query to list fields with checkboxes for the user selection of ones to include in a query result. It was working in the template file (based on page.php). Then I decided to move the logic that I will be using in other templates to a db-functions.php file. Now, I'm getting

Fatal error: Call to a member function get_results() on null in /home/...

printf($the_db) returns the proper login credentials, but apparently an error because 'affected_rows' = -1.

I can't find what's missing or misconfigured. Code is below.

require_once($_SERVER['DOCUMENT_ROOT'] . $folder . '/wp-config.php');
require_once($_SERVER['DOCUMENT_ROOT'] . $folder . '/wp-load.php');

//db-setup
function dbsetup($usr, $passwd, $databas, $hst){
    global $wpdb;
    $the_db = new wpdb($usr, $passwd, $databas, $hst);
}

//disp-field-list

function dbfldfrm(){
    $fieldlstress = $the_db->get_results("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.Columns where TABLE_NAME = 'index_records'");
Qirel
  • 25,449
  • 7
  • 45
  • 62
sediger
  • 13
  • 3

1 Answers1

2

Before asking, please search for solution on this site, with the error string. Btw, you need to globalise the variable from local scope to be available globally:

function dbsetup($usr, $passwd, $databas, $hst){
   global $wpdb;
   global $the_db;
   $the_db = ...
   // same as:   $GLOBALS['the_db'] = ...

and secondly, to obtain from global scope, do the same:

function dbfldfrm(){

    global $the_db;
    .....
T.Todua
  • 53,146
  • 19
  • 236
  • 237
  • I did search for and found several solutions, none of which worked (I had already tried most of those suggestions). I did have global @the_db in the file, but must have deleted it in the code sample to simplify the sample. This is still not working – sediger Oct 29 '18 at 21:37
  • @sediger I've updated the answer, it should work now. – T.Todua Oct 29 '18 at 21:55
  • Yes, that did it. I marked your's as the correct answer. Thanks T.Todua! – sediger Nov 06 '18 at 18:13