Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Getting data from non silverstripe DB throws 500 error


Reply


372 Views

Avatar
Darkfaythe

Community Member, 6 Posts

11 December 2012 at 11:05am

I am trying to get data from a non silverstripe DB to work on my server. It works fine in my local environment but not on my VPS. Both installs are using Ver 3 and all I get back is the standard 500 error return of "Sorry, there was a problem handling your request." which doesn't really help me as to where I should be looking.

And I use the following methodology:

###############################################################

// mysite/_config.php

global $nonSSDatabaseConfig;
$nonSSDatabaseConfig = array(
'type' => 'MySQLDatabase',
'server' => 'localhost',
'username' => 'usernameB',
'password' => 'passwordB',
'database' => 'databaseB'
);

Create a function to output the required data

// mysite/code/Example_Controller.php

class Example_Controller extends Page_Controller {

function BuildDataFromNonSSDB() {

global $databaseConfig;
global $nonSSDatabaseConfig;

// connect to non-ss DB
DB::connect($nonSSDatabaseConfig);

// create a query... this will query the non-ss database
// for more on SQLQuery() uses see: http://doc.silverstripe.org/sapphire/en/reference/sqlquery
$query = new SQLQuery();
$query->select = array(
"data1",
"data2",
"data3"
);
$query->from("table_name");
$query->where("data_live='1'");
$query->orderby("data1 ASC");

// execute the query to use the data ;-)
$result = $query->execute();

//move the SQL result data into the DataObjectSet
$dos = new DataObjectSet();
foreach($result as $DataRow) {
$dos->push(new ArrayData($DataRow));
}

// when done with database, switch back to the regular DB Config
DB::connect($databaseConfig);

// return the DataObjectSet to use in the template
return $dos;

}

}

In the template the database table field names are used as control variables (alias names can also be used.)

// themes/themeName/templates/LayoutExample_Controller.ss

<% if BuildDataFromNonSSDB%>
<table>
<% control BuildDataFromNonSSDB%>
<tr>
<td>$data1</td>
<td>$data2</td>
<td>$data3</td>
</tr>
<% end_control %>
</table>
<% end_if %>

#####################################################

Any suggestions would be greatly appreciated.

Regards

Rob