我有带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'"
我需要做些什么才能从文本中获取变量?
谢谢!
答案 0 :(得分:1)
include
指令正在评估包含文件,但范围有限。当您从函数内部包含文件时,只能访问此函数的本地变量。如你所见:
public function include_file($query_file)
{ include($query_file);
var_dump($request);
}
您之前只有一个局部变量,包括:$query_file
。
我建议手动将变量导入到本地范围。您可以在此处看到一些示例:https://stackoverflow.com/a/10144260/925196