We are proud to share our inclusion in the 2024 Gartner Magic Quadrant for Data Integration Tools. We believe this recognition reflects the differentiated business outcomes CData delivers to our customers.
Get the Report →Natively Connect to Sage 50 UK Data in PHP
The CData ODBC driver for Sage 50 UK enables you to create PHP applications with connectivity to Sage 50 UK data. Leverage the native support for ODBC in PHP.
Drop the CData ODBC Driver for Sage 50 UK into your LAMP or WAMP stack to build Sage 50 UK-connected Web applications. This article shows how to use PHP's ODBC built-in functions to connect to Sage 50 UK data, execute queries, and output the results.
Configure a DSN
If you have not already, first specify connection properties in an ODBC DSN (data source name). This is the last step of the driver installation. You can use the Microsoft ODBC Data Source Administrator to create and configure ODBC DSNs.
Note: Only Sage 50 UK 2012 and above are supported.
The User and Password properties, under the Connection section, must be set to valid Sage 50 UK user credentials. These values will be the same used to log in to the Sage 50 UK software.
Additionally, the URL property, under the Connection section, will need to be set to the address of the company dataset desired. To obtain the address, do the following:
- If you have not already done so, open the Sage 50 UK software.
- Click Tools -> Internet Options.
- Select the SData Settings tab.
- Click the Details button next to Sage 50 Accounts. A window is displayed containing a list of company names along with the address to their corresponding datasets.
- Set the URL property to the value in the address field next to the company desired.
Establish a Connection
Open the connection to Sage 50 UK by calling the odbc_connect or odbc_pconnect methods. To close connections, use odbc_close or odbc_close_all.
$conn = odbc_connect("CData ODBC Sage50UK Source","user","password");
Connections opened with odbc_connect are closed when the script ends. Connections opened with the odbc_pconnect method are still open after the script ends. This enables other scripts to share that connection when they connect with the same credentials. By sharing connections among your scripts, you can save system resources, and queries execute faster.
$conn = odbc_pconnect("CData ODBC Sage50UK Source","user","password");
...
odbc_close($conn); //persistent connection must be closed explicitly
Create Prepared Statements
Create prepared statements and parameterized queries with the odbc_prepare function.
$query = odbc_prepare($conn, "SELECT * FROM TradingAccounts WHERE TradingAccountUUID = ?");
Execute Queries
Execute prepared statements with odbc_execute.
$conn = odbc_connect("CData ODBC Sage50UK Source","user","password");
$query = odbc_prepare($conn, "SELECT * FROM TradingAccounts WHERE TradingAccountUUID = ?");
$success = odbc_execute($query, array('c2ef66a5-a545-413b-9312-79a53caadbc4'));
Execute nonparameterized queries with odbc_exec.
$conn = odbc_connect("CData ODBC Sage50UK Source","user","password");
$query = odbc_exec($conn, "SELECT Name, FinanceBalance FROM TradingAccounts");
Process Results
Access a row in the result set as an array with the odbc_fetch_array function.
$conn = odbc_connect("CData ODBC Sage 50 UK data Source","user","password");
$query = odbc_exec($conn, "SELECT Name, FinanceBalance FROM TradingAccounts");
while($row = odbc_fetch_array($query)){
echo $row["Name"] . "\n";
}
Display the result set in an HTML table with the odbc_result_all function.
$conn = odbc_connect("CData ODBC Sage 50 UK data Source","user","password");
$query = odbc_prepare($conn, "SELECT * FROM TradingAccounts WHERE TradingAccountUUID = ?");
$success = odbc_execute($query, array('c2ef66a5-a545-413b-9312-79a53caadbc4'));
if($success)
odbc_result_all($query);
More Example Queries
You will find complete information on the driver's supported SQL in the help documentation. The code examples above are Sage 50 UK-specific adaptations of the PHP community documentation for all ODBC functions.