title: How to connect on dynamic database in php using online cPanel database
body: <p>In my web app I have 2 different connections the main connection and the dynamic database.I used the main connection to create new database with tables and I used dynamic to switch database when user is already login.I already did this in localhost it works perfectly.But my problem is when I change it to Online server cPanel. I can't login and connect to dynamic database.
  Here is my code below: </p>
  
  <p><strong>create_database.php:</strong></p>
  
  <pre><code>$curl_path = "";
  
  function createDatabase($dbName, $dbUser, $dbPass,$userID,$userFullname,$userEmail,$userClinicname,$userAddress,$userCliniclogo,$username,$userpass) {
  
      $cpanel_user = "name";
      $cpanel_password = "pass";
      $cpanel_host = "mocha3020.mochahost.com";
      $cpanel_skin = "paper_lantern";
  
      $db_name = $dbName;
      $db_username = $dbUser;
      $db_userpass = $dbPass;
  
      $user_id = $userID;
      $user_full_name = $userFullname;
      $user_email = $userEmail;
      $user_clinic_name = $userClinicname;
      $user_address = $userAddress;
      $user_clinic_logo = $userCliniclogo;
      $user_name = $username;
      $user_pass =$userpass;
  
      $result = execCommand("http://$cpanel_user:$cpanel_password@$cpanel_host:2082/frontend/$cpanel_skin/sql/addb.html?db=$db_name");
      // create user
      $result .= execCommand("http://$cpanel_user:$cpanel_password@$cpanel_host:2082/frontend/$cpanel_skin/sql/adduser.html?user={$db_username}&amp;pass={$db_userpass}");
      // assign user to database
  
      $result.= execCommand("http://$cpanel_user:$cpanel_password@$cpanel_host:2082/frontend/$cpanel_skin/sql/addusertodb.html?user={$cpanel_user}_{$db_username}&amp;db={$cpanel_user}_{$db_name}&amp;privileges=ALL");
  
      include ('dynamicConfigs.php'); 
      $DB_con = dynamicDatabase($db_username,$db_userpass,$db_name);
      include('create_table.php'); 
  }
  
  function execCommand($command) {
      global $curl_path;
  
      if (!empty($curl_path)) {
          return exec("$curl_path '$command'");
      }
  
      else {
          return file_get_contents($command);
      }
  }
  </code></pre>
  
  <p>In <strong>class.user.php:</strong></p>
  
  <pre><code>public function login($uname,$upass) {
  
      try {
          $stmt = $this-&gt;db-&gt;prepare("SELECT user_id, user_name, user_pass, user_clinic_name, db_username, db_pass FROM users WHERE user_name=:uname LIMIT 1");
          $stmt-&gt;execute(array(':uname'=&gt;$uname));
          $userRow=$stmt-&gt;fetch(PDO::FETCH_ASSOC);
          if($stmt-&gt;rowCount() &gt; 0) {  
  
              if(password_verify($upass, $userRow['user_pass'])) {
  
                  if($userRow['user_Status'] != "Y"){
                      header('Location: index.php?inactive');     
                      exit;
                  }
  
                  $_SESSION['user_session'] = $userRow['user_id'];
                  $_SESSION['username'] = $uname;
  
                  $registrationDate = date_format('Y-m-d');
                  $currentDate = new DateTime($registration_Date);
                  $editedcurrent = date_format($currentDate, 'Y-m-d');
                  $expDate = $userRow['expiration_Date'];
  
                  if ($expDate &lt; $editedcurrent) {
                      header('Location:index.php?expiredUser');
                      exit;
                  }
  
                  $dbUsername = $userRow['user_name'];                        
                  $dbPassword = $userRow['user_pass'];
                  $dbName = $userRow['user_clinic_name'];
                  //update database   
                  include('dynamicConfigs.php');                       
                  $DB_con = dynamicDatabase($uname,$upass,$dbName); 
  
                  /*$random = password_hash(rand(1,20), PASSWORD_DEFAULT);
                  setcookie('logged_in' , $random, strtotime('+1 years'));
                  $stmt =  $this-&gt;db-&gt;prepare('UPDATE users  SET logged_in = 1, Cookie_Value = :cookie WHERE user_id = :user_id ');
                  $stmt-&gt;execute(array(':cookie' =&gt; $random, ':user_id'=&gt; $userRow['user_id']));
                  $editedClinic = strtolower(str_replace(' ', '_', $userRow['user_clinic_name']));  
                  */
                  header('Location: Home.php');                                          
               } 
  
               else {
                  header('Location: index.php?error'); 
                  exit;
               }
          }
  
          else {
              header('Location: index.php?error'); 
              exit;
          }
      }
  
      catch(PDOException $e) {
          echo $e-&gt;getMessage();
      }
  }
  </code></pre>
  
  <p><strong>connection.php</strong></p>
  
  <pre><code>$DB_host = "xxx.xx.xx.x2.";
  $DB_user = "prophics_dentAid";
  $DB_pass = "password";
  $DB_name = "prophics_dentiaide";    
  
  try {   
      $DB_con = new PDO("mysql:host=$DB_host;dbname=$DB_name",$DB_user,$DB_pass);
      $DB_con-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  
  }
  
  catch(PDOException $e) {
      echo $sql . "&lt;br&gt;" . $e-&gt;getMessage();
  }
  
  include_once 'class.user.php';
  $user = new USER($DB_con);
  </code></pre>
  
  <p><strong>dynamicConfig.php</strong></p>
  
  <pre><code>session_start();
  function dynamicDatabase($user_Db,$user_Pass,$db_name) {
  
      $DB_host = "xxx.xx.xx.x2";
      $DB_user = 'prophics_'.$user_Db;
      $DB_pass =  $user_Pass;
      $DB_name = 'prophics_'.$db_name;
  
      try {
              $DB_con = new PDO("mysql:host=$DB_host;dbname=$DB_name",$DB_user,$DB_pass);
              $DB_con-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
              return $DB_con;
  
      }  
  
      catch(PDOException $e) {    
          echo "Connection failed: " . $e-&gt;getMessage();
      }
  }
  </code></pre>
  
  <p><strong>index.php</strong></p>
  
  <pre><code>require 'connection.php';
  if (isset($_COOKIE['logged_in'])) {
      $cookieValue = $_COOKIE['logged_in'];
      $sth = $DB_con-&gt;prepare("SELECT user_id FROM users WHERE Cookie_Value = 
  :cookieValue " );
      $sth-&gt;execute(array(':cookieValue' =&gt; $cookieValue));
      $userRow=$sth-&gt;fetch(PDO::FETCH_ASSOC);
      if ($userRow != "") {
          $user -&gt; checkCookies($userRow['user_id']);
          exit;
      } 
  
      else {
          DisplayIndex();
      }
  } 
  
  else {
      DisplayIndex();
  }
  
  function DisplayIndex() {
      require 'connection.php';
      if(isset($_POST['btn-login'])) {
          $uname = $_POST['txt_uname'];
          $upass = $_POST['txt_password'];
          $user -&gt; login($uname, $upass);   
      }
  </code></pre>
  
  <p>Thank's and advance.</p>