0

I have php file with sql query, where are the php variables:

<?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'"; 
  ?>`

Also I have class with function, which call the content of the file:

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);  
} }

and Call the class:

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

My probles is: When I execute the query there are no variable, it can not to find them in my file. THis is the result of 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'"

What I have to do for GET my variables from text?

Thank you!

2
  • Can you clarify where the variables $select_1, $select_2, etc are being set? Commented Aug 13, 2013 at 17:17
  • $select_1, $select_2 are before the function with this file: <?php $select_1='something'; $select_2='something'; include('date_box.php'); $query_names = 'C:\\apache\\htdocs\\menue\\product\\queries'; $obj = new SelectOption($query_names); $obj->get_diretory($query_names); ?> Commented Aug 14, 2013 at 6:51

1 Answer 1

1

include directive is evaluating included file but in limited scope. When you're including file from inside a function, only variables local to this function are accesible for included file. And as you see:

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

You have only one local variable here before including: $query_file.

I would recommend manual importing of variables to local scope. You can see some example here: https://stackoverflow.com/a/10144260/925196

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.