Welcome Guest, Not a member yet? Register   Sign In
Query problem with alias
#1

[eluser]shinokada[/eluser]
Can I use alias?
I get an query error.
How can I use alias in CI?


Code:
function generateTree(&$tree, $parentid = 0) {
    $this->db->select('M.*, P.name AS PageName');
    $this->db->from('FROM menus AS M')
    $this->db->join('pages AS P', 'P.id = M.page_id');
    $this->db->where ('parentid',$parentid);
        $this->db->order_by('M.order asc, M.parentid asc');
    $res = $this->db->get('menus');
...
...
#2

[eluser]Jeroen Brussich[/eluser]
Please post more information.
You give as almost nothing to work with...

Read this excellent post about asking good questions.

But for now, try adding FALSE as parameter so your query won't be protected by backticks:
Code:
$this->db->select('M.*, P.name AS PageName', FALSE);
#3

[eluser]shinokada[/eluser]
I want to join a pages table and menu table.

Code:
CREATE TABLE IF NOT EXISTS `pages` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL DEFAULT '',
  `keywords` varchar(255) NOT NULL DEFAULT '',
  `description` varchar(255) NOT NULL DEFAULT '',
  `path` varchar(255) NOT NULL DEFAULT '',
  `content` text NOT NULL,
  `status` enum('active','inactive') NOT NULL DEFAULT 'active',
  `category_id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=21 ;


Code:
CREATE TABLE IF NOT EXISTS `menus` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `shortdesc` varchar(255) NOT NULL,
  `page_id` varchar(60) NOT NULL,
  `status` enum('active','inactive') NOT NULL,
  `parentid` int(11) NOT NULL,
  `order` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `page_id` (`page_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=79 ;

I have errors with the following SQL in a model.
If I take out join part, it works, but not with join.

How can I join them?

Code:
function generateTree1(&$tree, $parentid = 0) {
    $this->db->select('*');
    $this->db->from('menus');
    $this->db->join('pages', 'menus.page_id = pages.id');
    $this->db->where('parentid', $parentid);
    $res = $this->db->get();
    if ($res->num_rows() > 0) {
             foreach ($res->result_array() as $r) {
            
                $tree[$r['id']] = $r;
                $tree[$r['id']]['children'] = array();
                $this->generateTree($tree[$r['id']]['children'],$r['id']);
             }
        }
    }




Theme © iAndrew 2016 - Forum software by © MyBB