Hexadecimal string cut when doing Insert with PDO

Asked

Viewed 63 times

2

Good afternoon, you guys! I would like some help from you to solve a problem that is happening to me. I have a hexadecimal string containing an images that should be added to the database, when I do the Insert with PDO without the bindValue, passing the variable value 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 cut, and the image gets corrupted.

How do I fix this? is it a setup? I’d like to use it all via bindValue.

  • 1

    Your field is a blob?

  • What type of field used to save the hexadecimal file?

  • @Allanandrade is a Blob field

  • 1

    See if it works: $stmt->bindValue( ':Arquivo', $this->Arquivo, PDO::PARAM_LOB);

1 answer

2


The rray that’s right.

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

The code would look 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 doesn’t work, add the code below before the 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: Writing BD files using PHP PDO

  • I did as mentioned above, and the same error persists, a difference about the article informed, is that the string is already ready to make a recording, from another application, only take the hexadecimal string with approximately 500kb and write it. With the PDO::PARAM_LOB parameter, it also did not work, the string is also cut.

  • I changed the answer, test again and tell me if it worked.

  • The string appears in the correct mode: 0xffd8ffe000104a46494600010100000100..., but when recording the beginning of the Sring is 0x3078666466386665303030303030346134363439

Browser other questions tagged

You are not signed in. Login or sign up in order to post.