Jump to:

22979 Posts in 11815 Topics by 2826 members

General Questions

SilverStripe Forums » General Questions » Getting data from non silverstripe DB throws 500 error

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

Page: 1
Go to End
Author Topic: 213 Views
  • Darkfaythe
    Avatar
    Community Member
    6 Posts

    Getting data from non silverstripe DB throws 500 error Link to this post

    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

    213 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.