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

  • MSSQL bigint Unix Timestamp to Datetime with milliseconds
  • Fast way to “flatten” hierarchy table?
  • T-SQL CASE statement relies on another CASE statement in same SELECT query
  • Defining a function in sql server
  • How to get the right order for creation of stored procedure, user-defined functions and triggers
  • SQL Server: Add seconds to a datetime field?
  • 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:





    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:

    $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";

    Here is the output of my last few attempts:




    One Solution collect form web for “How to display 'text' data correctly with Microsoft ODBC driver for Linux? Currently displaying as random characters”

    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 🙂

    MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.