Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting random field name when exporting mysql to csv
    primarykey
    data
    text
    <p>I am trying to export a mysql table to csv. The issue I am having is it puts in a field name called 'id' every time the code increments. So after the first field, I get id. Two more fields after that, I get id. Three fields after that, I get id field. I have no idea why that would happen. Any help is greatly appreciated. (Note: I know not to use mysql, and I generally use mysqli, but this is what I am using for right now). Here is my code:</p> <pre><code> $hostname = "localhost"; //SET SERVER/HOSTNAME $dbusername = "username"; //SET DATABASE USERNAME $dbname = "dbname"; //SET DATABASE NAME $dbpassword = "password"; //SET DATABASE USERNAME $dbhandle = mysql_connect($hostname, $dbusername, $dbpassword) or die("Unable to connect to MySQL"); $selected = mysql_select_db($dbname,$dbhandle) or die("Could not select Data Base"); header("Content-type: application/csv"); header("Content-Disposition: attachment; filename=test.csv"); header("Pragma: no-cache"); header("Expires: 0"); $query = "SELECT * FROM v88374"; $export = mysql_query ($query ) or die ( "Sql error : " . mysql_error( ) ); $fields = mysql_num_fields ( $export ); for ( $i = 0; $i &lt; $fields; $i++ ) { $header .= mysql_field_name( $export , $i ) ."|" . "\t"; echo $header; } while( $row = mysql_fetch_row( $export ) ) { $line = ''; foreach( $row as $value ) { if ( ( !isset( $value ) ) || ( $value == "" ) ) { $value = "\t"; } else { $value = str_replace( '"' , '""' , $value ); $value = $value . '|' . "\t"; } $line .= $value; } $data .= trim( $line ) . "\n"; } $data = str_replace( "\r" , "" , $data ); if ( $data == "" ) { $data = "\n(0) Records Found!\n"; } print "$header\n$data"; exit(); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload