zohab Posted July 22, 2012 Share Posted July 22, 2012 Hi, I want to create result set from group by column. I have parent_title as group by columns I want to create array something like parent1 = > 0 => 0=>name1 1=>[email protected] 2=>parent1 1 => 0=>name2 1=>[email protected] 2=>parent1 parent2 = > 0 => 0=>name3 1=>[email protected] 2=>parent2 1 => 0=>name4 1=>[email protected] 2=>parent2 Thanks Zohaib. DROP TABLE IF EXISTS `tablename`; CREATE TABLE `tablename` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(300) DEFAULT NULL, `email` varchar(300) DEFAULT NULL, `parent_title` varchar(300) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1; /*Data for the table `tablename` */ insert into `tablename`(`id`,`name`,`email`,`parent_title`) values (1,'name1','[email protected]','parent1'),(2,'name2','[email protected]','parent1'),(3,'name3','[email protected]','parent2'),(4,'name4','[email protected]','parent2'); Link to comment https://forums.phpfreaks.com/topic/266069-create-array-from-result-set/ Share on other sites More sharing options...
Barand Posted July 22, 2012 Share Posted July 22, 2012 Try $sql = "SELECT name, email, parent_title FROM tablename"; $res = mysql_query($sql); $arr = array(); while ($r = mysql_fetch_row($res)) { $arr[$r[2]][] = $r; } echo '<pre>'.print_r($arr, 1).'</pre>'; Link to comment https://forums.phpfreaks.com/topic/266069-create-array-from-result-set/#findComment-1363425 Share on other sites More sharing options...
ignace Posted July 22, 2012 Share Posted July 22, 2012 If you use PDO then you can set the result to FETCH_GROUP this will automatically do what you want: http://blog.stealth35.com/2011/08/17/pdo-fetch-group.html Link to comment https://forums.phpfreaks.com/topic/266069-create-array-from-result-set/#findComment-1363480 Share on other sites More sharing options...
zohab Posted July 23, 2012 Author Share Posted July 23, 2012 Thanks Barand and ignace Link to comment https://forums.phpfreaks.com/topic/266069-create-array-from-result-set/#findComment-1363625 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.