使用存储过程,php和mysqli将image / longblob上传到mysql数据库

时间:2018-08-09 08:16:46

标签: php mysql mysqli blob

  

我正在尝试将图像上传到我的数据库,我知道将图像上传到数据库是一种不好的做法。我通过在php类(数据库处理程序)中运行查询来使其工作。现在,我正在尝试使用存储过程,但似乎在存储过程中找不到我的错误。抱歉,如果我的问题含糊。

DB Handler.php

public static function Business_Logo_Upload($image)
    {
        $sp = 'CALL uspBusines_Logo_Upload(?)';
        return DBHelper::BlobUpload($sp,$image);
    }

DBHelper.php

public static function BlobUpload($query,$image)
    {
        //Open Connection
        $conn = DB::Connect();
        //Prepare Query
        $call = $conn->prepare($query);
        $null = null;
        $call->bind_param('b', $null);
        $call->send_long_data(0, file_get_contents($image));
        $call->execute();
        //Close Connection
        mysqli_close($conn);
        //Return Result
        return $call;
    }

MYSQL存储过程

CREATE DEFINER=`BIT1`@`%` PROCEDURE `uspBusines_Logo_Upload`(IN logo LONGBLOB)
BEGIN
    DECLARE errno INT;
    DECLARE EXIT HANDLER FOR SQLEXCEPTION
    BEGIN
    GET CURRENT DIAGNOSTICS CONDITION 1 errno = MYSQL_ERRNO;
    SELECT errno AS MYSQL_ERROR;
    ROLLBACK;
    END;

    START TRANSACTION;
    SET autocommit=0;

    UPDATE business
    SET business.Logo = logo
    WHERE business.BusinessID = 1;
END

1 个答案:

答案 0 :(得分:0)

CREATE DEFINER=`BIT1`@`%` PROCEDURE `uspBusines_Logo_Upload`(IN logo LONGBLOB)
BEGIN
    UPDATE business
    SET business.Logo = logo
    WHERE business.BusinessID = 1;
END
  

这是解决方案。必须删除交易。