/usr/portage

PHP magic stuff 2

Sometimes I feel like being one of the little who like PHP. But, one things which brings me to hell currently is the mysqli-extension. Not that it is not easy to use, there is one strange thing I’m fucked up with. If I read an Image-file via file_get_contents() and want to write it to a database, It’s sensible to use a BLOB-field. So far so fine. I guessed I have to do it this way:

$image_data = file_get_contents( "my_image.png" );
$database = new mysqli( "host", "user", "password" );
$database->select_db( "my_db" );
if( $statement = $database->prepare( "INSERT INTO my_table SET my_blob = ?" ) ) {
   <strong>$statement->bind_param( "b", $image_data );</strong>
   $statement->execute();
} else {
etc.

The important line is marked bold. It assignes my binary data as a BLOB to the placeholder »?« in mysqli::prepare(). So I guessed nothing wrong with it. But it seems to be wrong. If I use $statement->bind_param( "s", $image_data);, which means passing the value as a string, it works as expected. Is this a bug or can someone explain this strange behaviour to me?


Update
I embarrass myself in my bugtracker entry as too lazy to read the entire documentation. This example cleared things up.

Filed under , , , , & two comments & no trackbacks

Trackbacks

Trackback specific URI for this entry

No Trackbacks

Comments

  1. Gerd Riesselmann returns:
    published on May 12th 2006, 02:06:33 pm *

    Maybe the file is larger than MAX_ALLOWED_PACKET?

    See here for an example how to upload a file into DB: http://php.net/manual/en/function.mysqli-stmt-send-long-data.php

    Reply

  2. Lars Strojny says:
    published on May 12th 2006, 04:53:13 pm *

    Guess that

    Reply

Add a Comment & let me know what you think