Page 1 of 1

PHP mssql question

Posted: Sat Oct 02, 2010 2:55 pm
by williamshen25
Hi all.

I am trying to use php to connect to a mssql express database and display an image. I used the following code.

$conn = mssql_connect('SHEN-PC\SQLEXPRESS', 'user', 'pass');

if (!$conn) {
die('Something went wrong while connecting to MSSQL');
}

$query = mssql_query('SELECT [Photo] FROM [social].[dbo].[Employers] where [Id] = 10');

header('Content-Type: image/jpg');
header('Content-Disposition: inline; filename="downloaded.jpg"');


// Check if there were any records
if (!mssql_num_rows($query)) {
echo 'No records found.';
} else {
// The following is equal to the code below:
//
// while ($row = mssql_fetch_row($query)) {

while ($row = mssql_fetch_object($query)) {
// ...
echo $row->Photo;
}
}

// Free the query result
mssql_free_result($query);


however, only the top 1/3 of the image got displayed..... I tried to print it out without header, and the raw code seems to be the same length regardless of what image i pull. so I think it might be because mssql didn't extract all the data? Can any one help me to figure out what s wrong with my code?

Re: PHP mssql question

Posted: Sat Oct 02, 2010 2:57 pm
by DigitalMind
Edit your post and use

Code: Select all

 for your code

Re: PHP mssql question

Posted: Sat Oct 02, 2010 6:13 pm
by williamshen25

Code: Select all

$conn = mssql_connect('SHEN-PC\SQLEXPRESS', 'user', 'pass');

if (!$conn) {
    die('Something went wrong while connecting to MSSQL');
}

$query = mssql_query('SELECT [Photo] FROM [social].[dbo].[Employers] where [Id] = 10');

header('Content-Type: image/jpg');
header('Content-Disposition: inline; filename="downloaded.jpg"');


// Check if there were any records
if (!mssql_num_rows($query)) {
    echo 'No records found.';
} else {
    // The following is equal to the code below:
    //
    // while ($row = mssql_fetch_row($query)) {

    while ($row = mssql_fetch_object($query)) {
        // ...
        echo $row->Photo;
    }
}

// Free the query result
mssql_free_result($query);

Re: PHP mssql question

Posted: Sat Oct 02, 2010 7:11 pm
by williamshen25
ok here is what I found after testing... apparently mssql didn't extract the full data from the database. I tested it by copy the image into an varchar data type and print out the data. The screen shows the same length of data no matter which image I print. Is this because of something I missed to do?

Re: PHP mssql question

Posted: Sat Oct 02, 2010 7:29 pm
by williamshen25
ok I find it... you need to either include

ini_set ( 'mssql.textlimit' , '2147483647' );
ini_set ( 'mssql.textsize' , '2147483647' );

in the beginning. or set php.ini 's mysql section according to above. just fyi