0

我有带有 sql 查询的 php 文件,php 变量在哪里:

<?php
$request = "SELECT
sp.product_id AS $select_1,
sp.last_price AS $select_2,
sp.bu_key AS $select_3
  FROM $stamp.supplier_product sp 
  WHERE  sp.bu_key = '$country'
  AND sp.product_id IN ('$textarea_1') 
  AND to_timestamp('$startDate', 'yyyy-mm-dd HH24:mi:ss.FF3') > sp.available_from_date
  AND (sp.available_thru_date > to_timestamp('$endDate', 'yyyy-mm-dd HH24:mi:ss.FF3') OR sp.available_thru_date is NULL)
  AND sp.packaging_id = 'NO_PACKAGING'"; 
  ?>`

我也有函数类,它调用文件的内容:

class GetContent {     
private $directory;
private  $query_file;

public function __construct($directory, $query_file) 
{ 

$this->query_file = $query_file;    
$this->directory = $directory;
}      public function get_content($query_file)   
{   
     file_get_contents($query_file);
     $content = file_get_contents($query_file);
     echo nl2br( htmlspecialchars($content));  
} 
  public function include_file($query_file)   
{   include($query_file);
     var_dump($request);  
} }

并打电话给班级:

<?php
include('date_box.php');
$query_names = 'C:\\apache\\htdocs\\menue\\product\\queries';
$obj = new SelectOption($query_names);
$obj->get_diretory($query_names);
?>

我的问题是:当我执行查询时没有变量,它无法在我的文件中找到它们。这是 var_dump($request); 的结果

string(416) "SELECT sp.product_id AS , sp.last_price AS , sp.bu_key AS FROM
supplier_product sp WHERE sp.bu_key = '' AND sp.product_id IN ('') 
AND to_timestamp('', 'yyyy-mm-dd HH24:mi:ss.FF3') > sp.available_from_date 
AND (sp.available_thru_date > to_timestamp('', 'yyyy-mm-dd HH24:mi:ss.FF3') 
OR sp.available_thru_date is NULL) AND sp.packaging_id = 'NO_PACKAGING'"

我必须做什么才能从文本中获取我的变量?

谢谢!

4

1 回答 1

1

include指令正在评估包含的文件,但范围有限。当您从函数内部包含文件时,只有该函数的本地变量才能被包含的文件访问。如您所见:

public function include_file($query_file)   
{   include($query_file);
    var_dump($request);  
}

在包括:$query_file.

我建议手动将变量导入本地范围。你可以在这里看到一些例子:https ://stackoverflow.com/a/10144260/925196

于 2014-05-23T09:35:29.013 回答