CodeIgniter Forums
Not able to run a query - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Not able to run a query (/showthread.php?tid=31657)



Not able to run a query - El Forum - 06-27-2010

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


Not able to run a query - El Forum - 06-27-2010

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


Not able to run a query - El Forum - 06-27-2010

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


Not able to run a query - El Forum - 06-28-2010

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