How to get data from a joined table
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.
Using * in the select you have 2 columns with the name "name". specify the * into the required columns rather (this will improve performance in any case) such as
c.name as categories_name
The categories.name
column is actually going to be returned as just $row['name']
in the result set, not $row['categories.name']
. Since products
also has a name
column, one is going to replace the other. Instead of selecting every field using the *
wildcard, you should specify which fields you want returned. For example:
SELECT p.*, c.name AS category
FROM products AS p
JOIN categories AS c
ON c.id = p.category_id
WHERE c.name = "Galleri1"
AND p.status = "active"
Then you can reference the category name as $row['category']
.
Because in your query you rename "categories" as "c"
Try changing:
"category" => $row['categories.name']
to
"category" => $row['c.name']
You shouldn't be using * either (as pointed out by someone else before I edited this) - that should have made it more obvious where the error was.
精彩评论