Welcome Guest, Not a member yet? Register   Sign In
Not able to run a query
#1

[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.
#2

[eluser]pickupman[/eluser]
I would make sure you have the correct path for read_file(). What happens if you echo $inserts?
#3

[eluser]shinokada[/eluser]
path is ok. When I output the data (without query)in a view, it shows all the content.
#4

[eluser]pickupman[/eluser]
At first glance, everything looked correct, and them I happened to remember you have trim the trailing ; at the end. explode() will create a empty array at the end of $contents. You can use this:
Code:
$contents = explode(';', rtrim($inserts,';'));

Explode on your string data will create an array like
[0] => Query 1
[1] => Query 2
[2] => NULL


That is why you are getting an empty query.




Theme © iAndrew 2016 - Forum software by © MyBB