简体   繁体   中英

Error exporting MySQL query to CSV

I am trying to export data to a cdv/xls file using PHP. I keep getting the following error:

*"Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in"*

Here is my code:

<?php


//Connect the the database 
require('../mysql_connect.php') ;

$select = "SELECT * FROM customers WHERE email = 'info@example.com'";

$export = mysql_query ( $select, $dbc ) or die ( "Sql error : " . mysql_error( ) );

$fields = mysql_num_fields ( $export );

for ( $i = 0; $i < $fields; $i++ )
{
    $header .= mysql_field_name( $export , $i ) . "\t";
}

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";                        
}

header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=export.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";


?>

Any help is greatly appreciated!

I was using mysqli and not mysql in my mysql_connect.php include. Thanks for the help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM