title: Php - using include/require_once inside function
body: <p>I am creating a class "<em>DBQuery</em>" that contains all the database query functions such as <strong>insert, select, delete, ...</strong> </p>
  
  <p>Everything is working fine when I create database connection inside the <strong>INSERT</strong> function. But i want separate the configuration so that i can include it in any other files and pages.</p>
  
  <p><strong>configuration.php</strong></p>
  
  <pre><code>define("HOSTNAME", "localhost");
  define("USERNAME", "root");
  define("PASSWORD", "");
  define("DATABASE", "edubits");
  
  try {
      $conn = new PDO("mysql:host=" . HOSTNAME . ";dbname=" . DATABASE . ";", USERNAME, PASSWORD);
      $conn-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      $conn-&gt;setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
  
  } catch (PDOException $e) {
      echo $e;
  }
  </code></pre>
  
  <p><strong>class.php</strong></p>
  
  <pre><code>/**
   * Created by PhpStorm.
   * User: Sunusi Mohd Inuwa
   * Date: 11/18/2018
   * Time: 11:02 AM
   */
  class QUERY
  {
      function INSERT($table, $data, $conn)
      {
          include_once('../configuration.php');
  
          // variable declaration
          $columns = "";
          $valueset = "";
          $values = "";
  
          //loop
          foreach ($data as $column =&gt; $value) {
              $columns = $columns . ', ' . $column;
              $valueset = $valueset . ', ?';
              $values = $values . ', ' . $value;
          }
  
          //trimming the first comma from the result above
          $columns = ltrim($columns, ',');
          $valueset = ltrim($valueset, ',');
          $values = ltrim($values, ',');
  
          //statement
          $sql = "INSERT INTO " . $table . "(" . $columns . ") VALUES(" . $valueset . ")";
  
          //convert values to array
          $values = explode(',', $values);
  
          //query
          $query = $conn-&gt;prepare($sql)-&gt;execute($values);
          //$query = $conn-&gt;prepare($sql)-&gt;execute([$values]);;
  
  
      }
  }
  </code></pre>