简体   繁体   English

在查询中使用$ get中的变量

[英]use variable from $get in query

Have a php scripts that holds 2 functions. 有一个PHP脚本,拥有2个功能。 In the first function the query is using the $get to fetch time and date. 在第一个函数中,查询使用$ get获取时间和日期。 The second function is making im trying to use the variable from the first function. 第二个函数使我试图使用第一个函数中的变量。

public function Execute( ) {


    if( $_GET['from'] && $_GET['to'] ){

        $from = $_GET['from'];
        $to = $_GET['to'];


       $query =  "SELECT * from historie_ordre where kode = ? AND time BETWEEN ? AND ? AND  order by time desc";
       $orders = DB::query( $query, $kode,  $from, $to )->fetchAll( DB::FETCH_ASSOC );


        Util::Debug( $orders  );
        $this->stats = $orders;

    }

    $this->kode = $kode;
    $this->from = $from;
    $this->to = $to;


}

second funstion: 第二功能:

     public function XLS(  ) {


 //trying to use this:
$from = intval($_GET['from']);
$to = intval($_GET['to']);
$kode = intval($_GET['kode']);



     $query =  "SELECT * from historie_ordre where kode = $kode AND time BETWEEN $from AND $to AND  order by time desc"; 

        $orders = DB::query( $query, $kode,  $from, $to )->fetchAll( DB::FETCH_ASSOC );


         $this->setTemplate( false );

         $oldreporting = error_reporting( 0 );

         require_once 'Spreadsheet/Excel/Writer.php';

// Creating a workbook
$workbook = new Spreadsheet_Excel_Writer();

// sending HTTP headers
$workbook->send('stabburet_stats.xls');

// Creating a worksheet
$worksheet =& $workbook->addWorksheet('My first worksheet');

// overskrift
//$worksheet->write(0, 0, 'Antall');
$worksheet->write(0, 1, 'kode');
$worksheet->write(0, 2, 'name');
$worksheet->write(0, 3, 'adress');


$i=1;
// for å få inn verdiane
foreach( $orders as $order ){
    $i++;
    //$worksheet->write($i, 0, $order['antall']);
    $worksheet->write($i, 1, $order['kode']);
    $worksheet->write($i, 2, utf8_decode( $order['name'] ) );
    $worksheet->write($i, 3, utf8_decode ($order['adress']) );


}        

// Sender fila
$workbook->close();
         error_reporting( $oldreporting );

      }



    }

The first function is working. 第一个功能正在运行。 second is only working with "hardcoding" the variables. 第二个是只对变量进行“硬编码”。

It was pointed out to you in comments you need to be more careful and use the same techniques consistently. 有人在评论中向您指出,您需要更加小心并始终使用相同的技术。 I also don't believe that the first function works , because your query syntax in each place is wrong. 我也不相信第一个函数有效 ,因为您在每个地方的查询语法都是错误的。

The problem at very least is that you have an incorrect SELECT syntax with a spurious AND at the end. 问题至少是,您使用了错误的SELECT语法,并且末尾带有虚假的AND。 You also are using a column named time, which is a mysql reserved word. 您还使用了名为time的列,这是mysql的保留字。 That might also be a problem, that can be mitigated by using the `column name` syntax. 这也可能是一个问题,可以通过使用`column name`语法来缓解。

 $query =  "SELECT * 
            from historie_ordre 
            WHERE kode = $kode 
                AND time BETWEEN $from AND $to AND  
            order by time desc";

Needs to be: 需要是:

  $query =  "SELECT * 
             FROM historie_ordre
             WHERE kode = ? 
             AND `time` BETWEEN ? AND ? 
             ORDER BY `time` DESC";

Updated: 更新:

Now that we have more information from you, the problem can also be traced to here: 既然我们从您那里获得了更多信息,问题也可以追溯到这里:

$from = intval($_GET['from']);
$to = intval($_GET['to']);
$kode = intval($_GET['kode']);

The problem is that your from and to need to be strings in date format. 问题是您的from和to必须是日期格式的字符串。 You are forcing them to be zeros by turning them into integers. 您通过将它们变成整数来强迫它们为零。 So again your query is not going to function. 因此,您的查询将再次失效。

$from = $_GET['from'];
$to = $_GET['to'];
$kode = intval($_GET['kode']);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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