Page 1 of 1

Insert image binary from xml data to mysql in PHP

Posted: Mon Feb 07, 2011 5:39 am
by satriaf
I have some photos (not big, only 8kb) in mysql database (in my desktop). the field type is blob. i want to export the table to xml file then upload it to my database website. but it not success. Here is what i have done : Exporting the data to xml (in my computer desktop):

Code: Select all

FileStream fs = new FileStream(filename,FileMode.Create,FileAccess.Write,FileShare.None);
StreamWriter sw = new StreamWriter(fs,Encoding.ASCII);
ds.WriteXml(sw);  //write the xml from the dataset ds
Then upload the xml from my joomla website. i load the xml before insert it to the database

Code: Select all

...
$obj = simplexml_load($filename);
$cnt = count($obj->mydata); //mydata is the table name in the xml tag
for($i=0;$i<cnt;$i++)
{
   ...
   $myphoto = 'NULL';
   if(!empty($obj->mydata[$i]->myphoto))
   {
      $myphoto = base64_code($obj->mydata[$i]->myphoto);
   }
   //insert to the database
   $sqlinsert = "insert into jos_myphoto (id,myphoto) values(".$i.",".$myphoto.")";
   ...
}
...
it keep telling me 'DB function failed'. when value of $myphoto is null, the query work well but if $myphoto is not null, the error appears. i think there is something wrong with the code

Code: Select all

$myphoto = base64_code($obj->mydata[$i]->myphoto)
. i try to remove base64_code function but it dont work. How to solve this problem? Sorry for my bad english