[eluser]shinokada[/eluser]
I am creating a cronjob.php
I have the following controller and model.
Code:
class Mycronjob extends Controller {
function Mycronjob(){
parent::Controller();
$this->load->model('Webshopmodel');
}
function index(){
// This runs cronjob every one hour or so.
// First empty/truncate the table
$this->Webshopmodel->empty_mytables();
// then enter new data
$data['inserts']= $this->Webshopmodel->insert_data();
$this->load->view('cronview', $data);
}
}
class Webshopmodel extends Model{
function Webshopmodel(){
parent::Model();
$this->load->database();
...
function insert_data(){
$inserts = read_file('assets/sql/demodatabase.txt');
// Turn each statement into an array item
$contents = explode(';', $inserts);
foreach ($contents as $content ){
$this->db->query($content);
$data[]=$content;
}
return $data;
}
I have a demodatabse.txt with the followings.
Code:
INSERT INTO `be_acl_groups` (`id`, `lft`, `rgt`, `name`, `link`) VALUES
(1, 1, 4, 'Member', NULL),
(2, 2, 3, 'Administrator', NULL);
INSERT INTO `be_acl_resources` (`id`, `lft`, `rgt`, `name`, `link`) VALUES
(1, 1, 46, 'Site', NULL),
(2, 26, 45, 'Control Panel', NULL),
(3, 27, 44, 'System', NULL),
(4, 38, 39, 'Members', NULL),
(5, 28, 37, 'Access Control', NULL),
(6, 40, 41, 'Settings', NULL),
...
...
I have a problem with function insert_data(). It gives an error "Query was empty"
If I change to this, then it works.
Code:
Change this
$contents = explode(';', $inserts);
foreach ($contents as $content ){
$this->db->query($content);
to this
$this->db->query("INSERT INTO `be_acl_groups` (`id`, `lft`, `rgt`, `name`, `link`) VALUES
(1, 1, 4, 'Member', NULL),
(2, 2, 3, 'Administrator', NULL)");
I tried this one, but it does not work either.
Code:
$inserts = read_file('assets/sql/demodatabase.txt');
// Turn each statement into an array item
$contents = explode(';', $inserts);
foreach ($contents as $content ){
$sql = '"'.$content.'"';
$this->db->query($sql);
I am hoping someone can give me advices how I can fix this.
Thanks in advance.