将MYSQL日期导出为CSV

时间:2013-07-25 06:00:59

标签: php mysql mysqli

我正在尝试将某些mysql记录导出到csv文件。我让用户选择日期范围,然后在这些日期之间显示屏幕上的记录。这样可行。我也可以将整个数据库导出到csv没问题,但是当我尝试根据日期导出csv文件时,它不起作用。 所以,这是我导出到csv代码:

header('Content-Disposition: attachment; filename="APGE_ELEC_2013MMDD.csv"');

    $hostname = "localhost"; //SET SERVER/HOSTNAME
    $dbusername = "ysername"; //SET DATABASE USERNAME
    $dbname = "test"; //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");

$query = "SELECT * FROM v88374 WHERE ((date >= '$from') AND (date <= '$to'))";

$export = mysql_query ($query ) 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";                        
}

print "$header\n$data";


exit();

现在,这是我的export.php文件。我有一个名为report.php的文件,它实际显示记录,然后调用export.php。这是代码:

<script type="text/javascript" language="javascript">
            jQuery(function() {
                jQuery( "#from" ).datepicker({
                  defaultDate: "+1w",
                  changeMonth: true,
                  numberOfMonths: 1,
                  dateFormat: "yy-mm-dd",
                  onClose: function( selectedDate ) {
                    $( "#to" ).datepicker( "option", "minDate", selectedDate );
                  }
                });
                jQuery( "#to" ).datepicker({
                  defaultDate: "+1w",
                  changeMonth: true,
                  numberOfMonths: 1,
                  dateFormat: "yy-mm-dd",
                  onClose: function( selectedDate ) {
                    jQuery( "#from" ).datepicker( "option", "maxDate", selectedDate );
                  }
                });
            });
</script>
<h1> Reporting Info</h1>
<form method="post">
<p>Select a date range: </p><label style="color:#FFF;" for="from">From</label>
<input type="text" id="from" name="from" />
<label style="color:#FFF;" for="to" >to</label>
<input type="text" id="to" name="to" />
<input name="export" type="submit" value="Sort" />
</form>
<?php
$from = $_POST['from'];
$to = $_POST['to'];
echo "from: " . $from;
echo " to: " . $to;
     $hostname = "localhost"; //SET SERVER/HOSTNAME
        $dbusername = "ysername"; //SET DATABASE USERNAME
        $dbname = "test"; //SET DATABASE NAME
        $dbpassword = "password"; //SET DATABASE USERNAME
    $link = mysqli_connect($hostname, $dbusername, $dbpassword, $dbname); 

    if (!$link)
    { 
        die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error()); 
    } 


    $sql = "SELECT * FROM v88374 WHERE date >= DATE_FORMAT('" . $from . "', '%Y/%m/%d') AND date <=  DATE_FORMAT('" . $to . "', '%Y/%m/%d')"; 
    //$sql = "SELECT * FROM v88374 WHERE date >= $from AND date <=  $to"; 
    //$result = mysqli_query($link, $sql, MYSQLI_USE_RESULT);
    if ($result = mysqli_query($link, $sql)) {
echo "
<table width=100% border='1px'>
<tr align='center' bgcolor='#999999'>
<td> First Name</td><td>Last Name</td><td>Phone Number</td><td>Address</td><td>City</td><td>Zip Code</td><td>Utility</td><td>Acct Num</td><td>Email</td><td>Validation Code</td><td>State</td><td>Date</td>
</tr>
";
    /* Get field information for all fields */
   while($row = $result->fetch_assoc()){
        $first_name = $row['first_name'];
        $last_name = $row['last_name'];
        $home_phone_num =  $row['home_phone_num'];
        $sline1_addr =  $row['sline1_addr'];
        $scity_name =  $row['scity_name'];
        $spostal_code =  $row['spostal_code'];
        $marketer_name =  $row['marketer_name'];
        $ldc_account_num =  $row['ldc_account_num'];
        $email =  $row['email'];
        $validation_code =  $row['validation_code'];
        $state =  $row['state'];
        $date =  $row['date'];

        echo " <tr align='center'><td> " . $first_name;
        echo "</td><td> " . $last_name;
        echo " </td><td> " . $home_phone_num;
        echo " </td><td> " . $sline1_addr;
        echo "</td><td> " . $scity_name;
        echo "</td><td> " . $spostal_code;
        echo " </td><td> " . $marketer_name;
        echo " </td><td>" . $ldc_account_num;
        echo " </td><td>" . $email;
        echo " </td><td> " . $validation_code;
        echo " </td><td>" . $state;
        echo " </td><td> " . $date;
        echo " </td></tr> ";
}
    }
    echo "</table>";


    echo "<form action='export.php'> <input type='submit' name='export' value= 'Export CSV'></form>";
/* close connection */
mysqli_close($link);

我希望我解释得够了哈哈。非常感谢任何帮助!

0 个答案:

没有答案