Question

I have one table that contains Company's Logo.... whenever I read small image like 4096 bytes It works perfectly, but i am not able to read over the 10000 bytes in php. it's read only 4096 bytes from database

Update: Column Lenth in Sql : enter image description here

My Code in Model

function GetLogoById($Id)
    {

        $this->load->database();
            mssql_query("SET TEXTSIZE 2147483647");

    // or use CI's active record
    $this->db->query("SET TEXTSIZE 2147483647");
        $query =  $this->db->query( "EXEC GetLogoById '$Id'" );
        $result = array();
        foreach ($query->result() as $row)
        {
            $result[] = array("Logo"=> $row->Logo);
        }

        return $result;
    }

My Controller Code :

public function GetLogoById()
    {

        parse_str($_SERVER['QUERY_STRING'],$_GET);
        $Id = $_GET['Id'];
        $this->load->model('MyModel');
        $result = $this->MyModel->GetLogoById($Id);
        header('Content-type: image/png');
        echo  $result[0]['Logo'];   

    }

I am Getting Only 4096 bytes like :(string:4096) ����

I am getting this error on browser : enter image description here

Was it helpful?

Solution 2

Got solution with sqlsrv driver ;)

 function GetLogoById($Id)
        {

            $serverName = "XXXXX";
            $uid = "XXXXXX";
            $pwd = "XXXXXX";

            $connectionInfo = array( "UID"=>$uid,
                    "PWD"=>$pwd,
                    "Database"=>"XXXXXX");
            $conn = sqlsrv_connect( $serverName, $connectionInfo);
            $tsql = "select Logo from TableName where Id = '$Id'";
            $stmt = sqlsrv_query( $conn, $tsql);
            if( $stmt === false)
            {
                echo "Error in query preparation/execution.\n";
                die( print_r( sqlsrv_errors(), true));
            }
            /* Retrieve each row as an associative array and display the results.*/
            while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC))
            {
                return $row;
            }
            /* Free statement and connection resources. */
            sqlsrv_free_stmt( $stmt);
            sqlsrv_close( $conn);

        }

OTHER TIPS

I think this is a problem with using MSSQL where data stored in a text type column is truncated for no apparent reason after 4096 characters.

Increase the maximum size of a text column to be returned from SQL Server. You can do this with the following SQL query:

SET TEXTSIZE 2147483647

You can run this with the following PHP, run it right after you make the connection:

mssql_query("SET TEXTSIZE 2147483647");

// or use CI's active record
$this->db->query("SET TEXTSIZE 2147483647");

And another way way to work around the issue is to change the "textlimit" and "textsize" settings within php.ini, like the following:

mssql.textlimit = 2147483647
mssql.textsize = 2147483647

Refer to this SO answer Why TEXT column returns only 4096 bytes? which refers to SQL Server, PHP and Truncating Text.

Update: Upon further review, reading this PHP bug, you may want to try using PDO_ODBC to connect to MSSQL Server instead:

//$dsn = 'mssql:host=MYBOX;dbname=testdb'; 
$dsn = 'odbc:DRIVER={SQL Server};SERVER=MYBOX;DATABASE=testdb;'; 

Also try setting the TEXTSIZE to megabytes:

$sql = "SET TEXTSIZE 3145728"; // 3 Megabytes
mssql_query($sql, $db) or die(mssql_get_last_message());

First. don't use the DBMS to store images. trust me.

Second, don't use the DMBS to store the images.....

Now, if you are getting an $id from the model, why have looping code to return an array? kind of defeats the purpose of an id?

I assume that the logo data is stored in the form of a BLOB ?

If this is the case the model could be something like:

$query = $this->db->get_where('tablename',array('id' => $id));
if ($query->num_rows() == 1)
{
    return $query->first_row()->logo
}
else
{
    return FALSE;
}

then the controller code could be something like:

  public function GetLogoById($id)
    {
        $this->load->model('MyModel');
        if($logo = $this->MyModel->GetLogoById($id))
        {
           $this->output->set_content_type('image/png');
           $this->output->set_output($logo);  
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top