zohab Posted May 5, 2011 Share Posted May 5, 2011 Hi, I have attached sql script for table structure and sample data. When I run following sql query ,I get result set. select * from tablename order by col10 desc I want to create array using result set. Following is the require structure of array. <?php $resultSet[]=array ( ('0') =>array ( ('col10value1') => array ( ('0') => array ( ('col1') => 'col1', ('col2') => 'col2', ('col3') => 'col3', ('col4') => 'col4', ('col5') => 'col5', ('col6') => 'col6', ('col7') => 'col7', ('col8') => 'col8', ), ('1') => array ( ('col1') => 'col1', ('col2') => 'col2', ('col3') => 'col3', ('col4') => 'col4', ('col5') => 'col5', ('col6') => 'col6', ('col7') => 'col7', ('col8') => 'col8', ), ), ), ('1') => array ( ('col10value2') => array ( ('0') => array ( ('col1') => 'col1', ('col2') => 'col2', ('col3') => 'col3', ('col4') => 'col4', ('col5') => 'col5', ('col6') => 'col6', ('col7') => 'col7', ('col8') => 'col8', ), ), ), ); ?> [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/235611-create-array-using-sql-query-result-set/ Share on other sites More sharing options...
wildteen88 Posted May 5, 2011 Share Posted May 5, 2011 To have that result you only need a few lines of code $resultSet = array(); while($row = mysql_fetch_assoc($query)) { $resultSet[] = $row; } echo "<pre>".print_r($resultSet,true)."</pre>"; Quote Link to comment https://forums.phpfreaks.com/topic/235611-create-array-using-sql-query-result-set/#findComment-1210972 Share on other sites More sharing options...
zohab Posted May 6, 2011 Author Share Posted May 6, 2011 This will create array but structure will not be the require one. Following is the require array structure. <?php $resultSet[]=array ( ('0') =>array ( ('col10value1') => array ( ('0') => array ( ('col1') => 'col1', ('col2') => 'col2', ('col3') => 'col3', ('col4') => 'col4', ('col5') => 'col5', ('col6') => 'col6', ('col7') => 'col7', ('col8') => 'col8', ), ('1') => array ( ('col1') => 'col1', ('col2') => 'col2', ('col3') => 'col3', ('col4') => 'col4', ('col5') => 'col5', ('col6') => 'col6', ('col7') => 'col7', ('col8') => 'col8', ), ), ), ('1') => array ( ('col10value2') => array ( ('0') => array ( ('col1') => 'col1', ('col2') => 'col2', ('col3') => 'col3', ('col4') => 'col4', ('col5') => 'col5', ('col6') => 'col6', ('col7') => 'col7', ('col8') => 'col8', ), ), ), ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/235611-create-array-using-sql-query-result-set/#findComment-1211311 Share on other sites More sharing options...
wildteen88 Posted May 6, 2011 Share Posted May 6, 2011 What is col10value1 and col10value2. Not sure what you're trying to do. Quote Link to comment https://forums.phpfreaks.com/topic/235611-create-array-using-sql-query-result-set/#findComment-1211492 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.