title: Make PDO connection variable accessible throughout site
body: <p>I am beginning to convert all old mysql_ functions to PDO and have a little trouble grasping this concept. I want to be able to access my PDO connection variable throughout my function pages so that it can be called as needed without making a connection script at the beginning of each function. I'll try to explain my setup as best as possible.</p>
  
  <p>This is my init.php page that loads all required php pages. This page is included at the top of each of my pages, but not function pages.</p>
  
  <p><strong>init.php</strong></p>
  
  <pre><code>&lt;?php
  session_start();
  
  error_reporting(E_All);
  
  require 'database/connect.php';
  require 'functions/users.php';
  require 'functions/general.php';
  require 'functions/trainer.php';
  ?&gt;
  </code></pre>
  
  <p>Here is my connect file that opens my db connection. Yes I have a <code>mysql_connect</code> open as well because I can not disable all my other functions while converting all the others over to PDO.</p>
  
  <p><strong>connect.php</strong></p>
  
  <pre><code>&lt;?php 
  
  $connect_error = 'Sorry there is a problem with the database connection.';
  mysql_connect('Localhost', 'customn7', 'I&lt;3deadlifts!') or die($connect_error);
  mysql_select_db('customn7_cm') or die($connect_error) or die($connect_error);
  ?&gt;
  
  &lt;?php   
  
  //PDO database connect
  $config['db'] = array(
      'host'      =&gt; 'Localhost',
      'username'  =&gt; 'customn7',
      'password'  =&gt; '********',
      'dbname'    =&gt; 'customn7_cm'
  );
  
  try {
  $db = new PDO('mysql:host=' .$config['db']['host']. ';dbname=' .$config['db']['dbname'], $config['db']['username'], $config['db']['password']);
  $db-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  $db-&gt;exec("SET CHARACTER SET utf8");
  } 
  
  catch(PDOException $e) {
  echo 'ERROR: ' . $e-&gt;getMessage();
  }
  
  ?&gt;
  </code></pre>
  
  <p>Here is my <code>functions/trainer.php</code> where the function is written:</p>
  
  <p><strong>trainer.php</strong></p>
  
  <pre><code>function exist_client_to_class($cd){
  list($user_id, $class_id, $first_name, $last_name, $nickname) = explode('|', $cd);
  
  try{
  
  $stmt = $db-&gt;prepare('INSERT INTO clients 
  (`user_id`, class_id, first_name, last_name, nickname, date)
  VALUES (:user_id, :class_id, :first_name, :last_name, :nickname, CURDATE())
  ');
  
  $stmt-&gt;execute(array(
          ':user_id' =&gt; $user_id, 
          ':class_id' =&gt; $class_id, 
          ':first_name' =&gt; $first_name,
          ':last_name' =&gt; $last_name,
          ':nickname' =&gt; $nickname)
          );
  }
  
  catch(PDOException $e) {
      echo 'Error: ' . $e-&gt;getMessage();
  }
  
  }
  </code></pre>
  
  <p>As of now I can not access my <code>$db</code> variable from my trainers.php page. Can someone help me with this?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Here is the code that calls the function:</p>
  
  <pre><code>// Post Selected name to current class.
  if (isset($_POST['exist_to_class'])){
  if (empty($_POST['client_data']) === true){
      $errors [] = 'You much select a client to be added to the class.';
  } else {
      if (isset($_POST['client_data']) &amp;&amp; !empty($_POST['client_data']));
      foreach ($_POST['client_data'] as $cd){
       exist_client_to_class($db, $cd);
       header('Location: view_class.php?class_id='.$class_id.' ');
      } // foreach $cd
  } // else
  
  } //isset
  </code></pre>