Search code examples
phpinclude

Is possible to use the Include function inside a php query?


Im trying to include a word (the name of a shop) that is in another php file into this query, i already tried in diferent ways but without success.

This is what i have:

<?
$queryentA2 = mysql_query("SELECT * FROM `client_invoices` WHERE 1 AND `paid` = 'ent' AND `date_due` > '2018-12-31' AND `shop` = 'include('shop.php')'");
$numberentA2 = mysql_num_rows($queryentA2);
?>

I expect the number of entries for that shop but no information is displayed.


Solution

  • $queryentA2 = mysql_query("SELECT * FROM `client_invoices` WHERE 1 AND `paid` = 'ent' AND `date_due` > '2018-12-31' AND `shop` = 'include('shop.php')'");
    

    You can't do that. There's a couple of way to do this

    1. Include a variable and then use that

      <?php
      $var = 'Some value';
      

      then

      $queryentA2 = mysql_query("SELECT * FROM `client_invoices` WHERE 1 AND `paid` = 'ent' AND `date_due` > '2018-12-31' AND `shop` = '" . $var . "'");
      
    2. Put the value as a naked string and then pull the whole file. So your include.php (or any file type) looks like

      Some Value
      

      then

      $var = mysql_real_escape_string(file_get_contents('include.php'));
      $queryentA2 = mysql_query("SELECT * FROM `client_invoices` WHERE 1 AND `paid` = 'ent' AND `date_due` > '2018-12-31' AND `shop` = '" . $var . "'");
      

    NOTE Normally you would want to use something like mysqli prepared statements. Since this is the older removed API (which means this is probably an end of life version) this poster likely has a myriad of other issues.