James Stoddern
James Stoddern

Reputation: 417

Sage 50 ODBC PHP encoding issues

I have setup the Sage Line 50 ODBC driver on my PC and am using XAMPP php server on the same machine to query the sage database with PHP.

Here is some basic code to interrogate the Sales Ledger table:

<?php
error_reporting(E_ERROR);

//attempt to connect to Sage Line 50 via the system DSN which is on the Sage Server
echo("Trying to connect to Sage<hr>");

if(isset($_REQUEST['account_ref']) && $_REQUEST['account_ref'] != "") {
$account_ref = $_REQUEST['account_ref'];

$odbc['dsn'] = "SAGE50";
$odbc['user'] = "manager";
$odbc['pass'] = "brand";

$conn = odbc_connect($odbc['dsn'], $odbc['user'], $odbc['pass']);
if (!$conn) {
    die("Error connecting to the ODBC database: " . odbc_errormsg());
} else {
    $obj = array();
    echo("Connected<br>");
    $query = odbc_exec($conn, "SELECT * FROM SALES_LEDGER where ACCOUNT_REF = '{$account_ref}'");

    while ($row = odbc_fetch_array($query)) {
        print_r($row);
    }
}
} else {
    return false;
}

Now, the query actually works and retrieves data. However it all looks garbage for the most part and has strange characters instead of values.

For example:

Array ( 
[�������] => [] => 
[0] => 0 
[Defaul] => Defaul 
[Open] => Open 
[T1] => T1 
[4000] => 4000 
[1] => 1 
[GB] => GB 
[0.00] => 0.00 
[30] => 30 
[2009-10-22] => 2009-10-22 
[Good] => Good 
[201x��201] => 201x��201 
[201���0�] => 201���0� 
) 

Can anyone offer any suggestions about why this may be happening and perhaps if there is a better way of doing this.

Any thoughts or experience would be welcomed.

Regards

James

Upvotes: 0

Views: 599

Answers (2)

Gaz Smith
Gaz Smith

Reputation: 1108

use array_map('utf8_encode', $row);

Upvotes: 0

MikeF
MikeF

Reputation: 477

I ran into exactly the same issue as this not so long ago. I can't tell you what causes it, but I can tell you that replacing Xampp for Wamp resolved it entirely.

Upvotes: 0

Related Questions