Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8805495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:52:56+00:00 2026-06-14T01:52:56+00:00

I am using PHP to query a MYSQL database, and I am trying to

  • 0

I am using PHP to query a MYSQL database, and I am trying to output the data to an excel file.

I have used phpexcel, csv, etc. With each i have successfully been able to output the data to an excel file, but I can’t seem to transpose it in the excel (transpose- flip all columns into rows, and rows into columns)

I can’t find any tutorials to help me out.

My question is, is there a way to use php to transpose those rows and columns from MYSQL and import them into an excel file?

thanks!

CODE:

exportMysqlToCsv($tablename,$tokenmain, $id);
function exportMysqlToCsv($tablename,$tokenmain, $id, $filename = 'Results.csv'){
    $sql_query = "select * from $tablename where token=1";

  // Gets the data from the database
   $result = mysql_query($sql_query);

    $f = fopen('php://temp', 'wt');
    $first = true;

//trying to transpose the data 
function transpose($array) {
    array_unshift($array, null);
    return call_user_func_array('array_map', $array);}

//inserting it into the excel file  
  while ($row = mysql_fetch_assoc($result)) {

        if ($first) {

            fputcsv($f, array_keys($row));

            $first = false;

         }

      fputcsv($f, $row);
    }
} //end while

Database sample:

=======================================
|  Male/female  |    Favorite artist  |
=====+========+===============+=======|
|    F          | Britney Spears      |
|-------------------------------------|

What I want it to look like:

=====================================
|Question          |  Answer        |
=====+========+===============+=====|
|   Male/female    |   F            |
|----+--------+---------------+-----|
| Favorite artist  | Britney Spears |
|----+--------+---------------+-----|

HUGE NOTE: There is no changing the database/hardcoding out the array in php. I want it all dynamic and the database is going to stay the way it is

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T01:52:57+00:00Added an answer on June 14, 2026 at 1:52 am

    To answer my question this is my code using PHPExcel:

    $col=1; // 1-based index
    while($row_data = mysql_fetch_assoc($result)) {
    
    $row= 1;
    
        foreach ($row_data as $key=>$value) {
    
    $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
    $row++;
        }
        $col++;
    
    }
    
    $row=1;
    while($row_data = mysql_fetch_assoc($result2)) {
    $col=0;
    
        foreach($row_data as $value) {
    
       $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
            $row++;
    
        $col++;}
    }
    

    Switch up the columns (col) and rows (row), so that the columns in the database become the rows in the excel file and the same for the rows to columns.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to export the content of mysql database to excel using
I'm trying to create a simple database table using the PHP MySQL query Create
i am trying to pass date to mysql query using php, but i am
I am trying to run a mysql insert query using php exec with parameters.
I have a simple query (in a mySQL view) that php is using to
I'm trying to fetch from a MySQL Database using PHP and at the moment
I have a webapp written in PHP using a MySQL database backend. This question
I'm trying to insert text into a MySQL DataBase from a form using PHP.
I am using php query to retrieve information from a mysql database with mysql_fetch_array
I am using PHP to query the MySQL database on my website. Please answer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.