I have two table joined with c.id = p.category_id. I want to get categories.name but it gives an error. Could anyone tell me how to get data from a joined table please?
function getGalleryone(){
$data = array();
$query = 'SELECT *
FROM products AS p
JOIN categories AS c
ON c.id = p.category_id
WHERE c.name = "Galleri1"
AND p.status = "active"' ;
$Q = $this->db->query($query);
/*
$this->db->select('*');
$this->db->where('categories.name','Galleri 1');
$this->db->where('products.status', 'active');
$this->db->join('categories', 'categories.id = products.category_id');
$this->db->order_by('name','random');
$Q = $this->db->get('products');
*/
if ($Q->num_rows() > 0){
foreach ($Q->result_array() as $row){
$data = array(
"id" => $row['id'],
"name" => $row['name'],
"shortdesc" => $row['shortdesc'],
...
...
"category" => $row['categories.name']
);
}
}
$Q->free_result();
return $data;
Database Products
CREATE TABLE IF NOT EXISTS `products` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`shortdesc` varchar(255) NOT NULL,
`longdesc` text NOT NULL,
`thumbnail` varchar(255) NOT NULL,
`image` varchar(255) NOT NULL,
`class` varchar(255) DEFAULT NULL,
`grouping` varchar(16) DEFAULT NULL,
`status` enum('active','inactive') NOT NULL,
`category_id` int(11) NOT NULL,
`featured` enum('true','false') NOT NULL,
`price` float(4,2) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=20 ;
Database Categories
CREATE TABLE IF NOT EXISTS `categories` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`shortdesc` varchar(255) NOT NULL,
`longdesc` text NOT NULL,
`status` enum('active','inactive') NOT NULL,
`parentid` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ;
...
...
Error message
A PHP Error was encountered
Severity: Notice
Message: Undefined index: categories.name
Filename: models/mproducts.php
Line Number: 111
Thanks in advance.