douniewei6346 2013-08-14 14:49
浏览 25
已采纳

PHP MySQL查询

I am having an issue with a SQL Query... I have a file that exports a CSV file which worked until i added a constraint to the query.

Here is what i have

<?php
 $today = date("mdY"); 
 function exportMysqlToCsv($table,$filename = 'TodaysSubmissions.csv')
 {
$csv_terminated = "
";
$csv_separator = ",";
$csv_enclosed = '"';
$csv_escaped = "\\";
$sql_query = "SELECT * FROM $table WHERE submitDate = '".$today."'";

I want to export all of the records from today only and in my form i am using the same date format for $today so it should match.

Also when i change to this it creates the CSV file exactly the way that i want

$sql_query = "SELECT * FROM $table WHERE submitDate = 08142013";

So what am i doing wrong when i call $today in the query? I feel like i have tried this every possible way.

Edit here is the entire file

            $today = date("mdY"); 
        function exportMysqlToCsv($table,$filename = 'TodaysSubmissions.csv')
        {
            $csv_terminated = "
";
            $csv_separator = ",";
            $csv_enclosed = '"';
            $csv_escaped = "\\";
            $sql_query = 'SELECT * FROM $table WHERE submitDate = "'.$today.'"';

            // Gets the data from the database
            $result = mysql_query($sql_query);
            $fields_cnt = mysql_num_fields($result);


            $schema_insert = '';

            for ($i = 0; $i < $fields_cnt; $i++)
            {
                $l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed,
                    stripslashes(mysql_field_name($result, $i))) . $csv_enclosed;
                $schema_insert .= $l;
                $schema_insert .= $csv_separator;
            } // end for

            $out = trim(substr($schema_insert, 0, -1));
            $out .= $csv_terminated;

            // Format the data
            while ($row = mysql_fetch_array($result))
            {
                $schema_insert = '';
                for ($j = 0; $j < $fields_cnt; $j++)
                {
                    if ($row[$j] == '0' || $row[$j] != '')
                    {

                        if ($csv_enclosed == '')
                        {
                            $schema_insert .= $row[$j];
                        } else
                        {
                            $schema_insert .= $csv_enclosed .
                            str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j]) . $csv_enclosed;
                        }
                    } else
                    {
                        $schema_insert .= '';
                    }

                    if ($j < $fields_cnt - 1)
                    {
                        $schema_insert .= $csv_separator;
                    }
                } // end for

                $out .= $schema_insert;
                $out .= $csv_terminated;
            } // end while

            header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
            header("Content-Length: " . strlen($out));
            // Output to browser with appropriate mime type, you choose ;)
            header("Content-type: text/x-csv");
            //header("Content-type: text/csv");
            //header("Content-type: application/csv");
            header("Content-Disposition: attachment; filename=$filename");
            echo $out;
            exit;
  • 写回答

2条回答 默认 最新

  • doushi8186 2013-08-14 14:56
    关注

    If you'd like to have the $today variable available to all your functions, and declare it outside the function, you have to declare it inside the function with global:

    <?php
    $today = date("mdY"); 
    
    ...
    
    function exportMysqlToCsv($table,$filename = 'TodaysSubmissions.csv')
    {
      global $today;
      ...
      $sql_query = "SELECT * FROM $table WHERE submitDate = '".$today."'";
    

    Recommended reading

    Also you have this in the "full" code:

    $sql_query = 'SELECT * FROM $table WHERE submitDate = "'.$today.'"';
    

    Double quotes won't work, that is for sure.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度