【问题标题】:PHP PDO load image into MS SQL Server 2012PHP PDO 将图像加载到 MS SQL Server 2012
【发布时间】:2013-09-27 11:18:05
【问题描述】:

我正在尝试使用 PDO 将图像上传到 SQL Server,其代码与我将其上传到 MySQL 中的代码相同,但没有成功。

我认为它会像在 MySQL 中一样简单,但微软确保我会受苦。

我收到的错误是:SQLSTATE[IMSSP]:将输入参数 3 的字符串转换为 UCS-2 时出错:目标多字节代码页中不存在 Unicode 字符的映射。

我创建了这个可运行的小示例(2 个文件“upload.php”和“test.html”)。 为了使用 MySQL,参数应该是 $db = 'mysql'

名为'upload.php'的php文件内容:

<?php

$db = 'mssql'; // $db = 'mysql';

$config = array(
    'mysql' => array(
        'dsn'       => 'mysql:host=localhost;dbname=',
        'user'      => 'root',
        'password'  => ''
    ),

    'mssql' => array(
        'dsn'       => 'sqlsrv:Server=localhost;Database=',
        'user'      => '',
        'password'  => ''
    )
);

try {
    // connect to database
    $pdoTest = new PDO($config[$db]['dsn'].'test', $config[$db]['user'], $config[$db]['password']);
    $pdoTest->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // insert image to table (with additional some info)
    $queryIn = $pdoTest->prepare("
        INSERT INTO     tbl_image (username, type, image)
        VALUES          (:username, :type, :image);
    ");
    $queryIn->bindValue(':username',  $_POST['username']);
    $queryIn->bindValue(':type',      $_FILES['image']['type']);
    $queryIn->bindValue(':image',     file_get_contents($_FILES['image']['tmp_name']));
    $queryIn->execute();

    // retrieve image and type from table
    $queryOut = $pdoTest->prepare("
        SELECT          type, image
        FROM            tbl_image
        WHERE           username = :username
    ");
    $queryOut->bindValue(':username',  $_POST['username']);
    $queryOut->execute();
    $row = $queryOut->fetch(PDO::FETCH_ASSOC);
    if ($row) {
        // send image back to browser
        header('content-type: '.$row['type']);
        echo $row['image'];
    }
}
catch (PDOException $e) {
    echo $e->getMessage();
}

名为“test.html”的html文件内容:

<!DOCTYPE html>
<html class="no-overflow">
    <head>
        <meta charset="UTF-8">

        <title>Load image to SQL Server 2012</title>
    </head>

    <body>
        <form enctype="multipart/form-data" action="upload.php" method="POST">
            <input type="text" name="username" autofocus="">
            <input type="file" name="image" accept="image/jpeg">
            <input type="submit" name="submit">
        </form>
    </body>
</html>

MySQL 定义:

database name     'test'
table name        'tbl_image'
field #1          'username varchar(255)'
field #2          'type varchar(255)'
field #3          'image blob'

SQL Server 2012 定义:

database name     'test'
table name        'tbl_image'
field #1          'username nvarchar(255)'
field #2          'type nvarchar(255)'
field #3          'image varbinary(MAX)'

我还尝试了一些旧的 SQL Server 示例,但没有成功:

$datastring = file_get_contents($_FILES['image']['tmp_name']);
$data       = unpack("H*hex", $datastring);
$image      = '0x'.$data;

有什么想法吗?

【问题讨论】:

    标签: php sql-server pdo


    【解决方案1】:

    您好,这是 mssql 的解决方案代码

    <?php
    
    $db = 'mssql';
    
    $config = array(
        'mysql' => array(
            'dsn'       => 'mysql:host=localhost;dbname=',
            'user'      => 'root',
            'password'  => ''
        ),
    
        'mssql' => array(
            'dsn'       => 'sqlsrv:Server=localhost;Database=',
            'user'      => '',
            'password'  => ''
        )
    );
    
    try {
        // connect to database
        $pdoTest = new PDO($config[$db]['dsn'].'test', $config[$db]['user'], $config[$db]['password']);
        $pdoTest->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
        // insert image to table (with additional some info)
        $queryIn = $pdoTest->prepare("
            INSERT INTO     tbl_image (username, type, image)
            VALUES          (:username, :type, :image);
        ");
        $queryIn->bindParam(':username',  $_POST['username']);
        $queryIn->bindParam(':type',      $_FILES['image']['type']);
        $queryIn->bindParam(':image',     file_get_contents($_FILES['image']['tmp_name']), PDO::PARAM_LOB, 0, PDO::SQLSRV_ENCODING_BINARY);
        $queryIn->execute();
    
        // retrieve image and type from table
        $queryOut = $pdoTest->prepare("
            SELECT          type, image
            FROM            tbl_image
            WHERE           username = :username
        ");
        $queryOut->bindParam(':username',  $_POST['username']);
        $queryOut->execute();
        $queryOut->bindColumn(2, $image, PDO::PARAM_LOB, 0, PDO::SQLSRV_ENCODING_BINARY);
        $row = $queryOut->fetch(PDO::FETCH_ASSOC);
        if ($row) {
            // send image back to browser
            header('content-type: '.$row['type']);
            echo $row['image'];
        }
    }
    catch (PDOException $e) {
        echo $e->getMessage();
    }
    

    【讨论】:

    • 这仅适用于 Windows,因为 PDO::SQLSRV_ENCODING_BINARY 常量仅在 sqlsrv 驱动程序中可用,而不是 dblib 驱动程序。仍在寻找unix中的解决方案...
    【解决方案2】:

    在实施了教我的工作解决方案后,我偶然发现了另一个使用 PHP 的 sqlsrv_connect 和 sqlsrv_query。

        $serverName = "<your_server_name>";
        $dbName = "<your_database>";
        $multipleActiveResultSets = "True";
    
        // Since UID and PWD are not specified in the $connectionInfo array,
        // The connection will be attempted using Windows Authentication.
        $connectionInfo = array( 
            "Database"=> $dbName, 
            "MultipleActiveResultSets"=> $multipleActiveResultSets        
            );
    
        $conn = sqlsrv_connect( $serverName, $connectionInfo);
    
        $query = "INSERT INTO <your_table> (image) VALUES (?)";
    
        $stmt = sqlsrv_query( $conn, $query, [file_get_contents($_FILES['image']['tmp_name']]);
    
        if( $stmt === false ) {
            die( print_r( sqlsrv_errors(), true));
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多