title: Variable $connection is undefined. Impossible, what is happening there?
body: <p><strong><em>Objective:</em></strong> I am in the process of creating a php login script.</p>
  
  <p><strong><em>Problem:</em></strong> I can't seem to be able to make my includes recognize the $connection variable even though it should be clearly defined in connection.php. As a result the value of my variable is nothing / NULL.</p>
  
  <p><strong><em>What I tried:</em></strong> I started with mysql but quickly noticed that it was the wrong approach and converted my code to mysqli. I checked for typos in all the $connection variables I have. I made sure the paths are correct. As a last resort I did a Google search but didn't find an answer or any useful hint to my scenario.</p>
  
  <p><strong><em>Problem:</em></strong> What is the reason for my variable not being defined?</p>
  
  <p><strong><em>Error Messages:</em></strong></p>
  
  <p><strong>All those messages are related to this single variable no being defined for some reason:</strong></p>
  
  <p><em>Php Notice: Undefined variable: connection in C:\xampp\htdocs\aspie\Php\Core\Common.php on line 4</em></p>
  
  <p><em>Php  Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\aspie\Php\Core\Common.php on line 4</em></p>
  
  <p><em>Php  Notice: Undefined variable: connection in C:\xampp\htdocs\aspie\Php\Core\Functions\Members.php on line 4</em></p>
  
  <p><em>Php Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\aspie\Php\Core\Functions\Members.php on line 4</em></p>
  
  <p><em>Php Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in C:\xampp\htdocs\aspie\Php\Core\Functions\Members.php on line 5</em> </p>
  
  <p><strong>// INITIALIZER</strong></p>
  
  <pre><code>&lt;?php
  session_start();
  // error_reporting(NULL);
  
  include 'Connection.php';
  include 'Common.php';
  include 'Functions/Members.php';
  
  ?&gt;
  </code></pre>
  
  <p>**// ERROR MESSAGES **</p>
  
  <pre><code>&lt;?php
  $connection_error = 'Our website is experiencing technical issues, please come back later.';
  $wrong_login = 'Password and name are wrong.';
  $member_registered = 'Access has been denied. You do not seem to be a registered user.';
  ?&gt;
  </code></pre>
  
  <p>**// CONNECTION **</p>
  
  <pre><code>&lt;?php
  include 'Errors.php';
  
  $connection = mysqli_connect('localhost', 'root', '', 'project') or exit ($connection_error);
  ?&gt;
  </code></pre>
  
  <p>**// COMMON **</p>
  
  <pre><code>&lt;?php
  error_reporting();
  function sanitize($connection, $data) {
      global $connection;
      global $data;
      return mysqli_real_escape_string($connection, $data);
  }
  ?&gt;
  </code></pre>
  
  <p>**// MEMBERS **</p>
  
  <pre><code>&lt;?php
  function member_registered($connection, $name) {
      $name = sanitize($connection, $name);
      $query = mysqli_query($connection, "SELECT COUNT(`id`) FROM `members` WHERE `name` = '$name'");
      return (mysqli_num_rows($query) == 1) ? true : false;
  }
  
  ?&gt;
  </code></pre>
  
  <p>**// LOGIN **</p>
  
  <pre><code>&lt;?php
  include 'Php/Core/Initializer.php';
  
      if (member_registered($connection, 'ee')) {
          echo "exists";
      }
      die("eee");
      echo error_reporting();
  if (empty($_POST) == false) {
      $name = $_POST['name'];
      $password = $_POST['name'];
  
      if (empty($name) OR empty($password)) {
          echo $wrong_login;
      }
  
      else if (member_registered($connection, $name) == false) {
          echo $member_registered;
      }
  }
  ?&gt;
  </code></pre>
  
  <p>UPDATE:</p>
  
  <p>Now what I get: existseee;</p>
  
  <p>The conditional doesnt work now even though its all set up right. Username doesnt exist so it should'nt echo "exists: </p>
  
  <pre><code>if (member_registered($connection, 'ee')) {
      echo "exists";
  }
  die("eee");
  </code></pre>