将图像插入sqlserver会产生“操作数类型冲突”

时间:2010-04-04 05:36:56

标签: php sql-server image

我正在尝试在sql server 2000数据库中保存图像。 该列的数据类型为image

我收到以下错误:

  

错误:警告:odbc_exec()   [function.odbc-exec]:SQL错误:   [Microsoft] [ODBC SQL Server   驱动程序] [SQL Server]操作数类型冲突:   text与图像,SQL不兼容   状态22005在SQLExecDirect中   C:\ xampp \ htdocs \ test \ upload.php on   第25行错误,查询失败

以下是代码:

图片上传:

<?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>";
    } 
?>

图片展示:

<?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']);
?>

我需要做些什么不同的事情?

1 个答案:

答案 0 :(得分:0)

此处至少有一个错误:base64_encode(fread(fopen(str_replace("'","''",$tmpName), "r"), $size)); 必须删除str_replace,因为它会破坏您的数据。

也可能存在其他错误。你必须调试你的代码。