Joe Reymann Wed Nov 14 14:10:29 -0500 2012

Subject: Binary Data

Hi,

I was wondering if there was a proper way to insert binary data using PHPActiveRecord? Maybe I'm doing it wrong... I experimented with hacking the Connection->query() function to use PDOStatement::bindParam. This way, I could pass in a hex representation, look for the leading '0x' as an indicator and set the bindParam type to binary. How have others dealt with binary data?

public function query($sql, &$values=array())
{
if ($this->logging)
$this->logger->log($sql);
$this->last_query = $sql;
try {
if (!($sth = $this->connection->prepare($sql)))
throw new DatabaseException($this);
foreach($values as $key => &$value)
{
$paramNum = $key+1;
if(is_string($value))
{
if(stripos($value, '0x') !== false) {
$binaryStr = pack("H*", substr($value, 2));
$sth->bindParam($paramNum, $binaryStr, PDO::PARAM_LOB);
}
else {
$sth->bindParam($paramNum, $value, PDO::PARAM_STR);
}
}
else if(is_int($value)) {
$sth->bindParam($paramNum, $value, PDO::PARAM_INT);
}
else if(is_null($value)) {
$sth->bindParam($paramNum, $value, PDO::PARAM_NULL);
}
else if(is_bool($value)) {
$sth->bindParam($paramNum, $value, PDO::PARAM_BOOL);
}
}
} catch (PDOException $e) {
throw new DatabaseException($e);
}
try{
$sth->setFetchMode(PDO::FETCH_ASSOC);
}catch (PDOException $e) {
throw new DatabaseException($this);
}
try {
$valid = $this->logging ? $this->profile_and_execute($sth, $sql, $values) : $sth->execute();
if (!$valid)
throw new DatabaseException($this);
} catch (PDOException $e) {
echo "
"; print_r($e); echo "
";
throw new DatabaseException($sth);
}
return $sth;
}