Inserting an image into sqlserver gives an "operand type clash"

Posted by Termedi on Stack Overflow See other posts from Stack Overflow or by Termedi
Published on 2010-04-04T05:36:55Z Indexed on 2010/04/06 21:03 UTC
Read the original article Hit count: 398

Filed under:
|
|

I'm trying to save an image in a sql server 2000 database. The data type of the column is image.

Here is the code:

Image Upload:

<?php

    include('config.php');

    if(is_uploaded_file($_FILES['userfile']['tmp_name']))
    {
        $fileName = $_FILES['userfile']['name'];
        $tmpName  = $_FILES['userfile']['tmp_name'];
        $fileSize = $_FILES['userfile']['size'];
        $fileType = $_FILES['userfile']['type'];

        $size = filesize($tmpName);
        set_magic_quotes_runtime(0);//to desactive the default escape spacials caracters made by PHP in the externes files

        $img_binaire = base64_encode(fread(fopen(str_replace("'","''",$tmpName), "r"), $size));

        $query = "INSERT INTO test_image (image_name, image_content, image_size) ".
        "VALUES ('{$fileName}','{$img_binaire}', '{$size}')";

        odbc_exec($conn, $query) or die('Error, query failed'); 

        echo "<br>File $fileName uploaded<br>";
        echo "<br>File Size: $fileSize <br>";
    } 
?>

Image Show:

<?php
    include('config.php');

    $sql = "select * from test_image where id =2";
    $rsl = odbc_exec($conn, $sql);
    $image_info = odbc_fetch_array($rsl);
    //$count = sizeof($image_info['image_content']);


    //header('Accept-Ranges: bytes');
    //header('Content-Length: '.$image_info['image_size']);
    //header("Content-length: 17397");
    header('Content-Type: image/jpeg'); 
    echo base64_decode($image_info['image_content']);

    //echo bindec($image_info['image_content']);
?>

It gives the following error:

Error: Warning: odbc_exec() [function.odbc-exec]: SQL error: [Microsoft][ODBC SQL Server Driver][SQL Server]Operand type clash: text is incompatible with image, SQL state 22005 in SQLExecDirect in C:\xampp\htdocs\test\upload.php on line 25 Error, query failed

What do I need to do differently?

© Stack Overflow or respective owner

Related posts about php

Related posts about sql-server