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=>name1@email.com 2=>parent1 1 => 0=>name2 1=>name2@email.com 2=>parent1 parent2 = > 0 => 0=>name3 1=>name3@email.com 2=>parent2 1 => 0=>name4 1=>name4@email.com 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','name1@email.com','parent1'),(2,'name2','name2@email.com','parent1'),(3,'name3','name3@email.com','parent2'),(4,'name4','name4@email.com','parent2'); Quote 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>'; Quote 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 Quote 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 Quote 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
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.