Insert BLOB into MSSQL using PHP

Recently I had a customer who wanted to push all VoiceMail voice files into a table in MSSQL database. After going through a lot of searching and trying I managed to get it running like this:

// This is the function which takes the filename and converts into HEX format.
function readSound($fileName) {
$dataString = file_get_contents($fileName );
$arrData = unpack("H*hex", $dataString);
$content = "0x".$arrData['hex'];     
return $content;
}
<strong>// This is where I pushed the sound file content into DB</strong>

function UpdateDB($unique_id,$content) {
    $link = mssql_connect('server','user','password');
    mssql_select_db('table');
<strong>    // The trick is that you should not enclose $content with ''</strong>
    $sql = "update Customer_Message set
        end_rec = getdate(),
        sound_msg = $content where unique_id='$unique_id'"
;
//    $sql = "sp_s_Update_VoiceMail '$unique_id', $content";
    debug("MS SQL Query: ****************************************",1);
    debug("$sql",1);

    $qry = mssql_query($sql);
    $res = mssql_fetch_array($qry);
//    debug("MS SQL Query Result: **$res**",1);
    print_r($res);
    mssql_close($link);

    return $res;
}

source:
http://www.php.net/manual/en/function.mssql-query.php#99746
http://stackoverflow.com/questions/574805/how-to-escape-strings-in-mssql-using-php

You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *