Hexadecimal string cut when inserting with PDO

2

Good afternoon everyone! I would like some help from you to solve a problem that is happening to me. I have a hexadecimal string containing an image that should be added to the database, when I insert it with the PDO without the bindValue, passing the value of the variable directly to the SQL string, works perfectly:

$sql = "INSERT INTO arquivos (arquivo) Value($this->Arquivo)";

try{
$stmt = $this->db->prepare( $sql );
//outros campos...
$stmt->execute();
}...

However, if I do via bindValue:

$sql = "INSERT INTO arquivos (arquivo) Value(:Arquivo)";
try{
$stmt = $this->db->prepare( $sql );
$stmt->bindValue( ':Arquivo', $this->Arquivo );
$stmt->execute();
}...

The value is trimmed, and the image becomes corrupted.

How do I fix this? Is it any configuration? I would like to use everything via bindValue.

    
asked by anonymous 09.01.2017 / 15:15

1 answer

2

The rray is right.

For large content (BLOB type for example), you must enter the data type: PDO::PARAM_LOB

The code looks like this:

$sql = "INSERT INTO arquivos (arquivo) Value(:Arquivo)";
try{
    $stmt = $this->db->prepare( $sql );
    $stmt->bindValue( ':Arquivo', $this->Arquivo, PDO::PARAM_LOB);
    $stmt->execute();
}

Other version:

$sql = "INSERT INTO arquivos (arquivo) Value(?)";
try{
    $stmt = $this->db->prepare( $sql );
    $stmt->bindParam(1, $this->Arquivo, PDO::PARAM_LOB);
    $stmt->execute();
}

If it does not work, add the code below before EXECUTE , and check if this property actually contains the correct string:

echo '<pre>';
var_dump($this->Arquivo);
echo '</pre>';

I suggest you read this article on the subject: Recording files in DB using PDO of PHP

    
09.01.2017 / 17:19