title: mysqli_connect() vs new PDO connecting and queries
body: <p><strong>//-------------The story so far-------------\</strong></p>
  
  <p>Hi all I am used to developing using the <code>mysqli_connect()</code> function to connect to the db like so:</p>
  
  <pre><code>&lt;?php  
  $db_host = "localhost"; 
  $db_username = "root";
  $db_pass = "pass";  
  $db_name = "db"; 
  
  $connect = mysqli_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
  mysqli_select_db($connect, "$db_name") or die ("no database");
  ?&gt;
  </code></pre>
  
  <p>When I connect in this way I always write my queries like:</p>
  
  <pre><code>$sql = mysqli_query($connect, "INSERT INTO table (column1, column2, column3) VALUES('$value1','$value2','$value3')") or die (mysql_error());
  </code></pre>
  
  <p>However I have recently been working on a project with a new hosting company who won't allow this. The above connection script will give a 500 error and when I asked the hosting company they said to connect using PDO which I have never done before.</p>
  
  <p>I am now able to connect using this script:</p>
  
  <pre><code>&lt;?php
  class DbConnect {
      const DB_HOSTNAME = 'localhost';
      const DB_USERNAME = 'root';
      const DB_PASSWORD = 'pass';
      const DB_DATABASE = 'db';
      public $error;
  
      public function connect() {
          try {
              $db = new \PDO("mysql:dbname=" . self::DB_DATABASE . ";host=" . self::DB_HOSTNAME, self::DB_USERNAME, self::DB_PASSWORD, array(PDO::MYSQL_ATTR_INIT_COMMAND =&gt; "SET NAMES utf8"));
              $db-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
          } catch (\PDOException $e) {
              $this-&gt;error = $e-&gt;getMessage();
              return FALSE;
          }
          return $db;
      }
  
      public function fail($message) {
          $out = "{$message}";
          return $out;
      }
  
      public function success($message) {
          $out = "{$message}";  
          return $out;
      }
  }
  
  $connect = new DbConnect;
  $db = $connect-&gt;connect();
  if ($db === FALSE) {
      echo $connect-&gt;fail($connect-&gt;error);
  } else {
      echo $connect-&gt;success("Connected to database");
  }
  ?&gt;
  </code></pre>
  
  <p>However, I am now trying to integrate this into an entire site and and am having some issues.</p>
  
  <p><strong>//-------------What I'm asking-------------\</strong></p>
  
  <p><strong>First of all</strong>, I just replaced one connection script with the other using <code>&lt;?php include_once("../connect_to_sql.php"); ?&gt;</code> (I didn't expect it to work just wanted to see what kind of errors I got) and besides a page full of errors I got the following at the very top. I thought if it was connecting by itself there should be nothing on my other pages where it is included that should cause the connection to fail:</p>
  
  <pre><code>SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES)
  </code></pre>
  
  <p><strong>Secondly</strong>, when I do get this connecting correctly inside my other files I don't want to have to change every single query I'm making. Will the <code>$connect</code> variable from the second connection file work the same way for the <code>mysqli_query()</code> function? Or what would be the best way to go about this where I would have to make minimal changes.</p>
  
  <p><strong>And finally</strong>, Can anyone explain the differences/benefits of connecting in this way or which way is better/more effecient?</p>
  
  <p>Any help or understanding anyone can share about this would be greatly appreciated.</p>