title: PHP and MySQLi check to see if the table exists and if it doesn't create it
body: <p>Attempting to use this to check to see if the table exists and if it doesn't create it</p>
  
  <pre><code>if ( !f_tableExists($table, DB_NAME) ) {
      $sql = "CREATE TABLE $table (
          ID int NOT NULL AUTO_INCREMENT,
          PRIMARY KEY(ID),
          timestamp int NOT NULL,
          ip int NOT NULL
      )";
  
      $result = mysqli_query($sql);
  
      if (!$result) {
          die('Invalid query: ' . mysqli_error());
      }
  
  }
  </code></pre>
  
  <p>where f_tableExists does the following to check if it exists.</p>
  
  <pre><code>function f_tableExists($tablename, $database = false) {
  
  if(!$database) {
      $res = mysqli_query("SELECT DATABASE()");
      $database = mysqli_data_seek($res, 0);
  }
  
  $res = mysqli_query("
      SELECT COUNT(*) AS count
      FROM information_schema.tables
      WHERE table_schema = '$database'
      AND table_name = '$tablename'   
  ");
  
  return mysqli_data_seek($res, 0) == 1;
  }
  </code></pre>
  
  <p>Getting the result 'Invalid query:'</p>
  
  <p>No table is being made and hence I assume some of my mysqli query code (CREATE TABLE....ip int NOT NULL) or (SELECT COUNT...= '$tablename') or another bit is wrong and/or not working? I don't know which bit or why?</p>