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

  • SEARCH
  • Home
  • 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 6171339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:11:45+00:00 2026-05-23T23:11:45+00:00

I’m trying to convert some MYSQL querys to MYSQLI, but I’m having an issue,

  • 0

I’m trying to convert some MYSQL querys to MYSQLI, but I’m having an issue, below is part of the script I am having issues with, the script turn a query into csv:

$columns = (($___mysqli_tmp = mysqli_num_fields($result)) ? $___mysqli_tmp : false); 

// Build a header row using the mysql field names 

$rowe = mysqli_fetch_assoc($result);
$acolumns = array_keys($rowe);
$csvstring = '"=""' . implode('""","=""', $acolumns) . '"""';
$header_row = $csvstring; 

// Below was used for MySQL, Above was added for MySQLi
//$header_row = '';
//for ($i = 0; $i < $columns; $i++) {
//  $column_title = $file["csv_contain"] . stripslashes(mysql_field_name($result, $i)) . $file["csv_contain"];
//  $column_title .= ($i < $columns-1) ? $file["csv_separate"] : '';
//  $header_row .= $column_title;
//  } 
$csv_file .= $header_row . $file["csv_end_row"]; // add header row to CSV file 

// Build the data rows by walking through the results array one row at a time 
$data_rows = ''; 
while ($row = mysqli_fetch_array($result)) { 
  for ($i = 0; $i < $columns; $i++) { 
    // clean up the data; strip slashes; replace double quotes with two single quotes 
    $data_rows .= $file["csv_contain"] .$file["csv_equ"] .$file["csv_contain"] .$file["csv_contain"] . preg_replace('/'.$file["csv_contain"].'/', $file["csv_contain"].$file["csv_contain"], stripslashes($row[$i])) . $file["csv_contain"] .$file["csv_contain"] .$file["csv_contain"];
    $data_rows .= ($i < $columns-1) ? $file["csv_separate"] : ''; 
  } 
  $data_rows .= $this->csv_end_row; // add data row to CSV file 
} 
$csv_file .= $data_rows; // add the data rows to CSV file 

if ($this->debugFlag) { 
  echo "Step 4 (repeats for each attachment): CSV file built. \n\n"; 
} 

// Return the completed file 
return $csv_file; 

The problem I am having is when building a header row for the column titles mysqli doesn’t use field_names so I am fetching the column titles by using mysqli_fetch_assoc() and then implode() the array, adding the ,‘s etc for the csv.

This works but when I produce the csv I am deleting the first data row when the header is active, when I remove my header part of the script and leave the header as null I get all data rows and a blank header (As expected).

So I must be missing something when joining my header to array to the $csv_file.

Can anyone point me in the right direction?

Many Thanks

Ben

  • 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-05-23T23:11:47+00:00Added an answer on May 23, 2026 at 11:11 pm

    A third alternative is to refactor the loop body as a function, then also call this function on the first row before entering the loop. You can use fputcsv as this function.

    $csv_stream = fopen('php://temp', 'r+');
    
    if ($row = $result->fetch_assoc()) {
        fputcsv($csv_stream, array_keys($row));
    
        fputcsv($csv_stream, $row);
        while ($row = $result->fetch_row()) {
            fputcsv($csv_stream, $row);
        }
    
        fseek($csv_stream, 0);
    }
    $csv_data = stream_get_contents($csv_stream);
    if ($this->debugFlag) { 
      echo "Step 4 (repeats for each attachment): CSV file built. \n\n"; 
    } 
    
    // Return the completed file
    return $csv_data;
    

    As this basically does the same thing as a do ... while loop, which would make more sense to use. I bring up this alternative to present the loop body refactoring technique, which can be used when a different kind of loop doesn’t make sense.

    Best of all would be to use both mysqli_result::fetch_fields and fputcsv

    $csv_stream = fopen('php://temp', 'r+');
    
    $fields = $result->fetch_fields();
    foreach ($fields as &$field) {
        $field = $field->name;
    }
    fputcsv($csv_stream, $fields);
    
    while ($row = $result->fetch_row()) {
        fputcsv($csv_stream, $row);
    }
    
    fseek($csv_stream, 0);
    $csv_data = stream_get_contents($csv_stream);
    if ($this->debugFlag) { 
      echo "Step 4 (repeats for each attachment): CSV file built. \n\n"; 
    } 
    
    // Return the completed file
    return $csv_data;
    

    If you can require that PHP be at least version 5.3, you can replace the foreach that generates the header line with a call to array_map. There admittedly isn’t much advantage to this, I just find the functional approach more interesting.

    fputcsv($csv_stream, 
            array_map(function($field) {return $field->name}, 
                      $result->fetch_fields()));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.