Welcome Guest, Not a member yet? Register   Sign In
Database backup and restore fontend
#1

[eluser]Dan Tdr[/eluser]
Hello,
I started working on a personal cms for my websites and i hit a point where i don`t know how to fix something.

As i mentioned in the title, i want to be able to backup and restore my database from within the cms, i managed to make a backup of my tables using $this->load->dbutil(); which is creating a file with all my tables and values.
The part that i can not get to work is the one to restore the backup from the backup file, i stumbled upon something while searching for an answer and managed to make this function:
Code:
function restorsql()
{
$schema = htmlspecialchars(file_get_contents('./resources/backupsql/2010-11-28.sql'));
$query = rtrim( trim($schema), "\n;");
$query_list = explode(";", $query);

foreach($query_list as $query)
     $this->db->query($query);  
}
it should work since the mysql backup is valid (checked it and it worked using phpmyadmin)
but when i run it i get this error:
Code:
A Database Error Occurred

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''Lorem ipsum dolor sit amet, consectetur adipiscing elit.s&' at line 1

INSERT INTO static (`static_id`, `static_type`, `static_nume`, `static_nume_en`, `static_descriere`, `static_descriere_en`, `static_more_url`, `static_more_url_en`) VALUES (2, 'bio', 'Biografie', 'Biography', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.s&

And here is what mysql backup file contains:
(i just left one table to be more aqurate with the error)
Code:
DROP TABLE IF EXISTS static;

CREATE TABLE `static` (
  `static_id` int(11) NOT NULL auto_increment,
  `static_type` varchar(30) NOT NULL,
  `static_nume` varchar(40) NOT NULL,
  `static_nume_en` varchar(40) NOT NULL,
  `static_descriere` text NOT NULL,
  `static_descriere_en` text NOT NULL,
  `static_more_url` varchar(40) NOT NULL,
  `static_more_url_en` varchar(40) NOT NULL,
  PRIMARY KEY  (`static_id`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

INSERT INTO static (`static_id`, `static_type`, `static_nume`, `static_nume_en`, `static_descriere`, `static_descriere_en`, `static_more_url`, `static_more_url_en`) VALUES (1, 'despre', 'Bun venit pe portofoliul meu oficial', 'Welcome to my official portfolio', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean tempus malesuada enim eu tempus. Cras suscipit dapibus arcu eget tincidunt. Pellentesque augue urna, commodo id volutpat nec, pellentesque id elit. Aenean sit amet erat ut nisl porttitor ullamcorper id et nisl. Duis eu accumsan sapien.', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean tempus malesuada enim eu tempus. Cras suscipit dapibus arcu eget tincidunt. Pellentesque augue urna, commodo id volutpat nec, pellentesque id elit. Aenean sit amet erat ut nisl porttitor ullamcorper id et nisl. Duis eu accumsan sapien.', 'despre-mine', 'about-me');
INSERT INTO static (`static_id`, `static_type`, `static_nume`, `static_nume_en`, `static_descriere`, `static_descriere_en`, `static_more_url`, `static_more_url_en`) VALUES (2, 'bio', 'Biografie', 'Biography', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.s<br /><br /> download PDF', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.s<br /><br /> download PDF', '', '');
INSERT INTO static (`static_id`, `static_type`, `static_nume`, `static_nume_en`, `static_descriere`, `static_descriere_en`, `static_more_url`, `static_more_url_en`) VALUES (3, 'contact', 'Contact', 'Contact', '<a href="mailto:[email protected]">[email protected]</a>', '<a href="mailto:[email protected]">[email protected]</a>', 'contact', 'contact-me');
As far as i see it i get an error because of the "&" character, i tried to escape it using \& but it had no effect, the & character comes from htmlencoded "<" i think.
I searched and tryed different solutions but i could not make it to work, can someone help me with this?
maybe i am overlooking something or there is a better simpler way to do it.
Once again i want to make the mysql restore available from the cms.

Thanks,
Dan
#2

[eluser]InsiteFX[/eluser]
Try htmlencode on the html string.
#3

[eluser]Dan Tdr[/eluser]
hey, thanks for the fast reply, i did try htmlencode and had no luck
any other ideas?
#4

[eluser]Unknown[/eluser]
I could not find and I wrote below.

Code:
$dosya = 'some-gz-file.gz';
$veri = gzfile($dosya);
foreach($veri as $i => $v){
    if(substr($v, 0 ,1) == '#' || trim($v) == '') unset($veri[$i]);
}
$yeni = explode(";\n", implode("\n", $veri));

foreach($yeni as $sql){
    if(trim($sql) != ''){
        $s = $this->db->query(trim($sql));
    }
}




Theme © iAndrew 2016 - Forum software by © MyBB