Ok so let me start off by saying I am not a NOOB.

There...

So i need help trying to figure out an export of just my 2 colums from a table. Got it done.

Now i need to make sure that I only export from a date range and the correct site.

Last it needs to be put into a CSV file.

I can get the file to save, and i can get the data in the csv file, but when i try to add a variable, it fails on me.

Here is the code:

<?php
function export_csv($site,$date1,$date2)
{
    $conn = mysql_connect("localhost","root","");
    $db = mysql_select_db("gearxs_customer",$conn);
    
    $sql = "SELECT id,tracking_number FROM customer_order WHERE site='$site' AND shipdate<= $date1 AND shipdate>= $date2";
    $rec = mysql_query($sql) or die (mysql_error());
    
    $num_fields = mysql_num_fields($rec);
    
    for($i = 0; $i < $num_fields; $i++ )
    {
        $header .= mysql_field_name($rec,$i)."\t";
    }
    
    while($row = mysql_fetch_row($rec))
    {
        $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 No Record Found!n";                        
    }
    
    header("Content-type: application/octet-stream");
    header("Content-Disposition: attachment; filename=reports.html");
    header("Pragma: no-cache");
    header("Expires: 0");
    print "$header\n$data";
}

export_csv("gearxs","2011-03-23","2011-03-23");
?>
Member Avatar for diafol

>but when i try to add a variable, it fails on me.

What do you mean add a variable? Where? You've got at least 14 different variables.

You can try:

$sql = "SELECT id,tracking_number FROM customer_order WHERE site='$site' AND shipdate<= '$date1' AND shipdate>= '$date2'";

Also

$header .= mysql_field_name($rec,$i)."\t";

Some servers don't like using the equals concatenator (.=) without the variable being defined. So perhaps you need $header=""; at the start of your code. Likewise for all the other .= cases.

>but when i try to add a variable, it fails on me.

What do you mean add a variable? Where? You've got at least 14 different variables.

You can try:

$sql = "SELECT id,tracking_number FROM customer_order WHERE site='$site' AND shipdate<= '$date1' AND shipdate>= '$date2'";

Also

$header .= mysql_field_name($rec,$i)."\t";

Some servers don't like using the equals concatenator (.=) without the variable being defined. So perhaps you need $header=""; at the start of your code. Likewise for all the other .= cases.

The first one worked wonderful after i have already done it. I ment to change this to solved as i figured it out yesterday. Thank you for your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.