Natively Connect to Sage US Data in PHP

Ready to get started?

Learn more:

Sage US Connectivity Solutions

The CData ODBC driver for Sage US enables you to create PHP applications with connectivity to Sage US data. Leverage the native support for ODBC in PHP.

Drop the CData ODBC Driver for Sage US into your LAMP or WAMP stack to build Sage US-connected Web applications. This article shows how to use PHP's ODBC built-in functions to connect to Sage US 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.

The Application Id and Company Name connection string options are required to connect to Sage as a data source. You can obtain an Application Id by contacting Sage directly to request access to the Sage 50 SDK.

Sage must be installed on the machine. The Sage.Peachtree.API.dll and Sage.Peachtree.API.Resolver.dll assemblies are required. These assemblies are installed with Sage in C:\Program Files\Sage\Peachtree\API\. Additionally, the Sage SDK requires .NET Framework 4.0 and is only compatible with 32-bit applications. To use the Sage SDK in Visual Studio, set the Platform Target property to "x86" in Project -> Properties -> Build.

You must authorize the application to access company data: To authorize your application to access Sage, restart the Sage application, open the company you want to access, and connect with your application. You will then be prompted to set access permissions for the application in the resulting dialog.

While the compiled executable will require authorization only once, during development you may need to follow this process to reauthorize a new build. To avoid restarting the Sage application when developing with Visual Studio, click Build -> Configuration Manager and uncheck "Build" for your project.

Establish a Connection

Open the connection to Sage US by calling the odbc_connect or odbc_pconnect methods. To close connections, use odbc_close or odbc_close_all.

$conn = odbc_connect("CData ODBC Sage50US 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 Sage50US 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 Customer WHERE Name = ?");

Execute Queries

Execute prepared statements with odbc_execute.

$conn = odbc_connect("CData ODBC Sage50US Source","user","password"); $query = odbc_prepare($conn, "SELECT * FROM Customer WHERE Name = ?"); $success = odbc_execute($query, array('ALDRED'));

Execute nonparameterized queries with odbc_exec.

$conn = odbc_connect("CData ODBC Sage50US Source","user","password"); $query = odbc_exec($conn, "SELECT Name, LastInvoiceAmount FROM Customer");

Process Results

Access a row in the result set as an array with the odbc_fetch_array function.

$conn = odbc_connect("CData ODBC Sage US data Source","user","password"); $query = odbc_exec($conn, "SELECT Name, LastInvoiceAmount FROM Customer"); 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 US data Source","user","password"); $query = odbc_prepare($conn, "SELECT * FROM Customer WHERE Name = ?"); $success = odbc_execute($query, array('ALDRED')); 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 US-specific adaptations of the PHP community documentation for all ODBC functions.