We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 10194
    • 16 Posts
    I’m currently trying to pull values for a Template Variable (Dropdown List) from an external database (which just so happens to be a MODx install as well). Both sites reside on the same server, and I’ve tried the following using a snippet and running it via the EVAL command.

    Unfortunately I get the following error:

    MODx encountered the following error while attempting to parse the requested resource:
    « Failed to select the database ’bcetb_modx’! »

    Is there a way to pull values from an external database into a template variable drop down list?

    Snippet:
    //$db = $modx->db->connect($host, $database, $uid, $pwd, true);
    $db = new DBAPI();
    $db->connect($host,$database, $uid,$pwd);
    
    $result = $db->select('id, pagetitle','site_content');
    	
    if( $db->getRecordCount( $result ) >= 1 ) 
    {
    	while( $row = $db->getRow( $result ) ) 
    	{
    		echo $row['id'].'||'.$row['pagetitle'];
    	}	
    }
    //disconnect
    $db->disconnect();
     
    //reconnect to modx db
    $modx->db->connect();
    

    Template Variable:
    @EVAL return $modx->runSnippet('etbDropdown');
    
      • 10194
      • 16 Posts
      Nevermind, I realized I was using the wrong method, since the database wasn’t on a different server I was able to pull data via the following

      $result = $modx->db->select('id, pagetitle','bcetb_modxetb.modx_site_content');
      //$result = $db->select('id, pagetitle','site_content');
      	
      if( $modx->db->getRecordCount( $result ) >= 1 ) 
      {
      	while( $row = $modx->db->getRow( $result ) ) 
      	{
      		echo $row['id'].'||'.$row['pagetitle'];
      	}	
      }