title: Error executing connecting to database and executing database query
body: <p>I am creating a log in for a website. index.php file controls what is displayed by checking the user details. It display a message if user is logged in else the login form. I have included the files connect.php and core.php. When I open index.php I get the following error. The variable con is already defined but it shows it is undefined please help me to find the mistake Thanks in advance.</p>
  
  <p><strong>Error:</strong></p>
  
  <pre><code>Notice: Undefined variable: con in C:\xampp\htdocs\includes\core.php on line 33
  
  Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\includes\core.php on line 33
  
  Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in C:\xampp\htdocs\includes\core.php on line 34 
  </code></pre>
  
  <p><strong>index.php</strong></p>
  
  <pre><code>&lt;?php
  require_once 'includes/connect.php' ;
  require_once 'includes/core.php' ;
  
  if(logged_in())
  {
      echo "You are logged in" ;
  }
  else
  {
  ?&gt;
      &lt;form action="login.php" method="post"&gt;
          &lt;table&gt;
              &lt;tr&gt;
                  &lt;td&gt;Email&lt;/td&gt;
                  &lt;td&gt;Password &lt;a href="resetpass.php"&gt;Forgot Password?&lt;/a&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td&gt;&lt;input type="email" name="sin_email" placeholder="Email" name="email"&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;input type="password" name="sin_pass" placeholder="Password" name="pass"&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;input type="submit" value="Log In"&gt;&lt;/td&gt;
              &lt;/tr&gt;
          &lt;/table&gt;
      &lt;/form&gt;
  &lt;?php
  }
  ?&gt;
  </code></pre>
  
  <p><strong>includes/core.php</strong></p>
  
  <pre><code>&lt;?php
  //Start output buffer.Stores all the output of the server into a stack
  ob_start() ;
  //Start a session
  session_start() ;
  //Get the name of current file
  $current_file=$_SERVER['SCRIPT_NAME'] ;
  //Get the referrer to this page if any
  if(isset($_SERVER['HTTP_REFERER']) &amp;&amp; !empty($_SERVER['HTTP_REFERER']))
  {
      $http_referrer=$_SERVER['HTTP_REFERER'] ;
  }
  
  function logged_in()
  {
      if(isset($_SESSION['login_token'],$_SESSION['user_name'],$_SESSION['user_id']) 
      &amp;&amp; !empty($_SESSION['login_token']) &amp;&amp; !empty($_SESSION['user_name']) &amp;&amp; !empty($_SESSION['user_id']))
      {
          //Store session variable to local variables
          $user_name=$_SESSION['user_name'] ;
          $user_id=$_SESSION['user_id'] ;
          $http_agent=$_SERVER['HTTP_USER_AGENT'] ;
          $ip_address=$_SERVER['REMOTE_ADDR'] ;
  
          //Recreating the token string
          $token_string=$user_name.$user_id.$http_agent.$ip_address ;
  
          //Obtaining the md5 value for the token_string
          $obtained_login_token=md5($token_string) ;
  
          //Fetching previous token_string stored in the database
          $token_query="SELECT * FROM securelogin WHERE user_id='$user_id' ;" ;
          $token_result=mysqli_query($con,$token_query) ;
          $token_info=mysqli_fetch_assoc($token_result) ;
          $stored_login_token=$token_info['login_string'] ;
  
          if($stored_login_token===$obtained_login_token)
          {
              return true ;
          }
          else
          {
              return false ;
          }
      }
      else
      {
          return false ;
      }
  }
  ?&gt;
  </code></pre>
  
  <p><strong>includes/connect.php</strong></p>
  
  <pre><code>&lt;?php
  $host_name="localhost" ;
  $user_name="root" ;
  $password="" ;
  $db_name="futsided" ;
  
  $con=mysqli_connect($host_name,$user_name,$password,$db_name)
  or
  die("Cannot connect to our server :(&lt;br/&gt;Please try again later or contact &lt;a href=\"/report.html\"&gt;Administrator&lt;/a&gt;") ;
  ?&gt;
  </code></pre>