Matt
Matt

Reputation: 251

How to display 'text' data correctly with Microsoft ODBC driver for Linux? Currently displaying as random characters

I am trying to display the contents of a field in a MS SQL database, the data type of which is 'text'. When I connect to the database with MS Excel on a PC the value looks something like:

Shipped on the: 18/10/12 Toll IPEC Connote Number: XXX XXX XXXX

When I connect to the database with PHP using the Microsoft ODBC driver for Linux, the output of the text field will display random characters, which are slightly different each time I run the exact same script. Here is what it output the last four times:

Xisep)!ØwXment.class.php))

5isep)!ment.class.php))

µ}isep)!Ø}ment.class.php))

t)!!Owner_IDt)Ø©Ø8

Not sure where it's getting the 'ment.class.php' bit from. That looks like the end of the name of one of my classes, but not one that is included in this particular script. Is the driver broken or what? All other data types (int, varchar, datetime etc) seem to display correctly, the problem only seems to happen with this one text field.

Here is the code:

<?php

require('ConnectWise.inc.php');

$config = new CW_Config;

// Connect to database
$dbcnx = odbc_connect("ConnectWise", $config->db_username, $config->db_password);

// Query database
$query = "select * from Billing_Log where Invoice_Number = '24011'";
$result = odbc_exec($dbcnx, $query);
while ($row = odbc_fetch_array($result)) {
    echo $row['Top_Comment'] . "\n";
}

odbc_close($dbcnx);

Here is the output of my last few attempts:

\3ȶä!!¶äY!

öÈö§!!ö§Y!

&Èö×!!ö×Y!

Upvotes: 1

Views: 353

Answers (1)

Matt
Matt

Reputation: 21

Looks like you're getting an overflow. For some reason SQL is passing the length of your text field as 0, which to SQL means "long" but to PHP means "I dont know" and so PHP is showing whatever is in its memory at the location its expecting data (or something like that - any PHP experts care to explain?).

I've seen this with varchar(max) before but not text. (Converting to) Text is normally the way to fix it. So maybe I'm wrong.

Hope this is of some help. Im not a PHP developer, I just have to use it occasionally, and this sounds much like a painful experience I've gone through before :)

Upvotes: 2

Related Questions