正在将大文本插入blob


Inserting large text into blob

我一直在努力寻找这个问题的解决方案,但在互联网上找不到答案。在这种情况下,我需要使用php从firebird数据库中插入或更新blob字段(子类型1)。问题是,当文本变得非常大>36k时,它将不会执行查询。我知道行查询仅限于32k的数据,我曾尝试在c#中使用参数化查询,但在PHP中找不到适合我的东西。甚至还没有接近工作。

我试过使用ibase_blob_Create等等,我试过直接插入Insert into table (blob_value) values (?)ibase_prepare等等。似乎没有什么对我有效。有什么神奇的方法可以在php中实现这一点吗?还是不可能从php将大文本放入blob?

我试过用之类的东西

class DBMgmt_Ibase_Blob extends DBMgmt_Generic_Blob
{
var $blob; // resourse link
var $id;
var $database;
function DBMgmt_Ibase_Blob(&$database, $id=null)
{
    $this->database =& $database;
    $this->id = $id;
    $this->blob = null;
}
function read($len)
{
    if ($this->id === false) return ''; // wr-only blob
    if (!($e=$this->_firstUse())) {
        return $e;
    }
    $data = @ibase_blob_get($this->blob, $len);
    if ($data === false) return $this->_setDbError('read');
    return $data;        
}
function write($data)
{
    if (!($e=$this->_firstUse())) return $e;
    $ok = @ibase_blob_add($this->blob, $data);
    if ($ok === false) return $this->_setDbError('add data to');
    return true;
}
function close()
{
    if (!($e=$this->_firstUse())) return $e;
    if ($this->blob) {
        $id = @ibase_blob_close($this->blob);
        if ($id === false) return $this->_setDbError('close');
        $this->blob = null;
    } else {
        $id = null;
    }
    return $this->id ? $this->id : $id;
}
function length()
{
    if ($this->id === false) return 0; // wr-only blob
    if (!($e=$this->_firstUse())) return $e;
    $info = @ibase_blob_info($this->id);
    if (!$info) return $this->_setDbError('get length of');
    return $info[0];
}
function _setDbError($query)
{
    $hId = $this->id === null ? "null" : ($this->id === false ? "false" : $this->id);
    $query = "-- $query BLOB $hId"; 
    $this->database->_setDbError($query);        
}
// Called on each blob use (reading or writing).
function _firstUse()
{
    // BLOB is opened - nothing to do.
    if (is_resource($this->blob)) return true;
    // Open or create blob.
    if ($this->id !== null) {
        $this->blob = @ibase_blob_open($this->id);
        if ($this->blob === false) return $this->_setDbError('open'); 
    } else {
        $this->blob = @ibase_blob_create($this->database->link);
        if ($this->blob === false) return $this->_setDbError('create');
    }
    return true;
}
}

我这样使用:

 $text = new DBMgmt_Ibase_Blob($DB);
            if (!$text->write(htmlspecialchars($Data["Text"]))){
                throw new Exception("blob fail");
            }
  $blobid = $text->close();
  //similar query
$DB->query("INSERT INTO TABLE1 (BLOB_VALUE) VALUES (?)",$blobid);

之后,在我的数据库中,我找到了一个我知道必须使用的号码:

Blob = new DBMgmt_Ibase_Blob($DB,$data->Text);
    $data->Text = $Blob->read($Blob->length());

但我得到的字符串不是一个BLOB ID,在数据库中有一个类似30064771072的数字。

我试图直接添加它,但它当然不能像这个那样工作

$DB->query('insert into table (blob) values (?)',$string); // where string is like 170k chars

我得到动态SQL错误SQL错误代码=-104命令行1第236列意外结束

我试着把它放进以下php.net参考php.net blob_import的文件中,代码如下:

$dbh = ibase_connect($host, $username, $password);
$filename = '/tmp/bar';
 $fd = fopen($filename, 'r');
if ($fd) {
$blob = ibase_blob_import($dbh, $fd);
fclose($fd);
if (!is_string($blob)) {
    // import failed
} else {
    $query = "INSERT INTO foo (name, data) VALUES ('$filename', ?)";
    $prepared = ibase_prepare($dbh, $query);
    if (!ibase_execute($prepared, $blob)) {
        // record insertion failed
    }
}
} else {
// unable to open

但我仍然得到像blob写方法一样的结果。

您需要使用参数:

$query = ibase_prepare($DB, 'insert into table (blob) values (?)');
ibase_execute($query, $string);

cf:http://php.net/manual/en/function.ibase-execute.php