title: how to establish database connection in kohana mvc framework
body: <p>I am trying to create a login/Signup page using Auth/ORM module of kohana. i have succesfully installed the MySQLi module and added a line in the bootstrap.php file. Now when I try to create an account with all the details entered i get an error stating ErrorException [ Notice ]: Trying to get property of non-object in line no. 67 of MySQLi.php file. I am using xampp/phpmyAdmin.</p>
  
  <p>the error is in line: ':code' => $this->_connection->connect_errno,</p>
  
  <p>
  
  <p>class Kohana_Database_MySQLi extends Database {</p>
  
  <pre><code>// Database in use by each connection
  protected static $_current_databases = array();
  
  // Use SET NAMES to set the character set
  protected static $_set_names;
  
  // Identifier for this connection within the PHP driver
  protected $_connection_id;
  
  // MySQL uses a backtick for identifiers
  protected $_identifier = '`';
  
  public function connect()
  {
      if ($this-&gt;_connection)
          return;
  
      if (Database_MySQLi::$_set_names === NULL)
      {
          // Determine if we can use mysqli_set_charset(), which is only
          // available on PHP 5.2.3+ when compiled against MySQL 5.0+
          Database_MySQLi::$_set_names = ! function_exists('mysqli_set_charset');
      }
  
      // Extract the connection parameters, adding required variabels
      extract($this-&gt;_config['connection'] + array(
          'database'   =&gt; 'kohana',
          'hostname'   =&gt; 'localhost',
          'port'       =&gt; ini_get("mysqli.default_port"),
          'socket'     =&gt; ini_get("mysqli.default_socket"),
          'username'   =&gt; FALSE,
          'password'   =&gt; FALSE,
          'persistent' =&gt; FALSE,
      ));
  
      // Prevent this information from showing up in traces
      //unset($this-&gt;_config['connection']['username'], $this-&gt;_config['connection']['password']);
  
      try
      {
          if ($persistent)
          {
              // Create a persistent connection
              $this-&gt;_connection = new MySQLi('p:'.$hostname, $username, $password, $database, $port, $socket);
          }
          else
          {
              // Create a connection and force it to be a new link
              $this-&gt;_connection = new MySQLi($hostname, $username, $password, $database, $port, $socket);
          }
      }
      catch (ErrorException $e)
      {
          throw new Database_Exception('[:code] :error', array(
                  **':code' =&gt; $this-&gt;_connection-&gt;connect_errno,**
                  ':error' =&gt; $this-&gt;_connection-&gt;connect_error,
              ), $this-&gt;_connection-&gt;connect_errno);
  
          // No connection exists
          $this-&gt;_connection = NULL;
      }
  
      // \xFF is a better delimiter, but the PHP driver uses underscore
      $this-&gt;_connection_id = sha1($hostname.'_'.$username.'_'.$password);
  
      $this-&gt;_select_db($database);
  
      if ( ! empty($this-&gt;_config['charset']))
      {
          // Set the character set
          $this-&gt;set_charset($this-&gt;_config['charset']);
      }
  }
  
  /**
   * Select the database
   *
   * @param   string  Database
   * @return  void
   */
  protected function _select_db($database)
  {
      if ( ! $this-&gt;_connection-&gt;select_db($database))
      {
          // Unable to select database
          throw new Database_Exception('[:code] :error', array(
              ':code' =&gt; $this-&gt;_connection-&gt;errno,
              ':error' =&gt; $this-&gt;_connection-&gt;error,
          ), $this-&gt;_connection-&gt;errno);
      }
  
      Database_MySQLi::$_current_databases[$this-&gt;_connection_id] = $database;
  }
  
  public function disconnect()
  {
      try
      {
          // Database is assumed disconnected
          $status = TRUE;
  
          if ($this-&gt;_connection instanceof mysqli)
          {
              if ($status = $this-&gt;_connection-&gt;close())
              {
                  // Clear the connection
                  $this-&gt;_connection = NULL;
              }
          }
  
          parent::disconnect();
      }
      catch (Exception $e)
      {
          // Database is probably not disconnected
          $status = ! ($this-&gt;_connection instanceof mysqli);
      }
  
      return $status;
  }
  
  public function set_charset($charset)
  {
      // Make sure the database is connected
      $this-&gt;_connection or $this-&gt;connect();
  
      if (Database_MySQLi::$_set_names === TRUE)
      {
          // PHP is compiled against MySQL 4.x
          $status = (bool) $this-&gt;_connection-&gt;query('SET NAMES '.$this-&gt;quote($charset));
      }
      else
      {
          // PHP is compiled against MySQL 5.x
          $status = $this-&gt;_connection-&gt;set_charset($charset);
      }
  
      if ($status === FALSE)
      {
          throw new Database_Exception('[:code] :error', array(
              ':code' =&gt; $this-&gt;_connection-&gt;errno,
              ':error' =&gt; $this-&gt;_connection-&gt;error,
          ), $this-&gt;_connection-&gt;errno);
      }
  }
  
  public function query($type, $sql, $as_object = FALSE, array $params = NULL)
  {
      // Make sure the database is connected
      $this-&gt;_connection or $this-&gt;connect();
  
      if ( ! empty($this-&gt;_config['profiling']))
      {
          // Benchmark this query for the current instance
          $benchmark = Profiler::start("Database ({$this-&gt;_instance})", $sql);
      }
  
      if ( ! empty($this-&gt;_config['connection']['persistent']) AND $this-&gt;_config['connection']['database'] !== Database_MySQLi::$_current_databases[$this-&gt;_connection_id])
      {
          // Select database on persistent connections
          $this-&gt;_select_db($this-&gt;_config['connection']['database']);
      }
  
      // Execute the query
      if (($result = $this-&gt;_connection-&gt;query($sql)) === FALSE)
      {
          if (isset($benchmark))
          {
              // This benchmark is worthless
              Profiler::delete($benchmark);
          }
  
          throw new Database_Exception('[:code] :error ( :query )', array(
              ':code' =&gt; $this-&gt;_connection-&gt;errno,
              ':error' =&gt; $this-&gt;_connection-&gt;error,
              ':query' =&gt; $sql,
          ), $this-&gt;_connection-&gt;errno);
      }
  
      if (isset($benchmark))
      {
          Profiler::stop($benchmark);
      }
  
      // Set the last query
      $this-&gt;last_query = $sql;
  
      if ($type === Database::SELECT)
      {
          // Return an iterator of results
          return new Database_MySQLi_Result($result, $sql, $as_object, $params);
      }
      elseif ($type === Database::INSERT)
      {
          // Return a list of insert id and rows created
          return array(
              $this-&gt;_connection-&gt;insert_id,
              $this-&gt;_connection-&gt;affected_rows,
          );
      }
      else
      {
          // Return the number of rows affected
          return $this-&gt;_connection-&gt;affected_rows;
      }
  }
  
  public function datatype($type)
  {
      static $types = array
      (
          'blob'                      =&gt; array('type' =&gt; 'string', 'binary' =&gt; TRUE, 'character_maximum_length' =&gt; '65535'),
          'bool'                      =&gt; array('type' =&gt; 'bool'),
          'bigint unsigned'           =&gt; array('type' =&gt; 'int', 'min' =&gt; '0', 'max' =&gt; '18446744073709551615'),
          'datetime'                  =&gt; array('type' =&gt; 'string'),
          'decimal unsigned'          =&gt; array('type' =&gt; 'float', 'exact' =&gt; TRUE, 'min' =&gt; '0'),
          'double'                    =&gt; array('type' =&gt; 'float'),
          'double precision unsigned' =&gt; array('type' =&gt; 'float', 'min' =&gt; '0'),
          'double unsigned'           =&gt; array('type' =&gt; 'float', 'min' =&gt; '0'),
          'enum'                      =&gt; array('type' =&gt; 'string'),
          'fixed'                     =&gt; array('type' =&gt; 'float', 'exact' =&gt; TRUE),
          'fixed unsigned'            =&gt; array('type' =&gt; 'float', 'exact' =&gt; TRUE, 'min' =&gt; '0'),
          'float unsigned'            =&gt; array('type' =&gt; 'float', 'min' =&gt; '0'),
          'int unsigned'              =&gt; array('type' =&gt; 'int', 'min' =&gt; '0', 'max' =&gt; '4294967295'),
          'integer unsigned'          =&gt; array('type' =&gt; 'int', 'min' =&gt; '0', 'max' =&gt; '4294967295'),
          'longblob'                  =&gt; array('type' =&gt; 'string', 'binary' =&gt; TRUE, 'character_maximum_length' =&gt; '4294967295'),
          'longtext'                  =&gt; array('type' =&gt; 'string', 'character_maximum_length' =&gt; '4294967295'),
          'mediumblob'                =&gt; array('type' =&gt; 'string', 'binary' =&gt; TRUE, 'character_maximum_length' =&gt; '16777215'),
          'mediumint'                 =&gt; array('type' =&gt; 'int', 'min' =&gt; '-8388608', 'max' =&gt; '8388607'),
          'mediumint unsigned'        =&gt; array('type' =&gt; 'int', 'min' =&gt; '0', 'max' =&gt; '16777215'),
          'mediumtext'                =&gt; array('type' =&gt; 'string', 'character_maximum_length' =&gt; '16777215'),
          'national varchar'          =&gt; array('type' =&gt; 'string'),
          'numeric unsigned'          =&gt; array('type' =&gt; 'float', 'exact' =&gt; TRUE, 'min' =&gt; '0'),
          'nvarchar'                  =&gt; array('type' =&gt; 'string'),
          'point'                     =&gt; array('type' =&gt; 'string', 'binary' =&gt; TRUE),
          'real unsigned'             =&gt; array('type' =&gt; 'float', 'min' =&gt; '0'),
          'set'                       =&gt; array('type' =&gt; 'string'),
          'smallint unsigned'         =&gt; array('type' =&gt; 'int', 'min' =&gt; '0', 'max' =&gt; '65535'),
          'text'                      =&gt; array('type' =&gt; 'string', 'character_maximum_length' =&gt; '65535'),
          'tinyblob'                  =&gt; array('type' =&gt; 'string', 'binary' =&gt; TRUE, 'character_maximum_length' =&gt; '255'),
          'tinyint'                   =&gt; array('type' =&gt; 'int', 'min' =&gt; '-128', 'max' =&gt; '127'),
          'tinyint unsigned'          =&gt; array('type' =&gt; 'int', 'min' =&gt; '0', 'max' =&gt; '255'),
          'tinytext'                  =&gt; array('type' =&gt; 'string', 'character_maximum_length' =&gt; '255'),
          'year'                      =&gt; array('type' =&gt; 'string'),
      );
  
      $type = str_replace(' zerofill', '', $type);
  
      if (isset($types[$type]))
          return $types[$type];
  
      return parent::datatype($type);
  }
  
  /**
   * Start a SQL transaction
   *
   * @link http://dev.mysql.com/doc/refman/5.0/en/set-transaction.html
   *
   * @param string Isolation level
   * @return boolean
   */
  public function begin($mode = NULL)
  {
      // Make sure the database is connected
      $this-&gt;_connection or $this-&gt;connect();
  
      if ($mode AND ! $this-&gt;_connection-&gt;query("SET TRANSACTION ISOLATION LEVEL $mode"))
      {
          throw new Database_Exception(':error', array(':error' =&gt; $this-&gt;_connection-&gt;error),
                                       $this-&gt;_connection-&gt;errno);
      }
  
      return (bool) $this-&gt;_connection-&gt;query('START TRANSACTION');
  }
  
  /**
   * Commit a SQL transaction
   *
   * @param string Isolation level
   * @return boolean
   */
  public function commit()
  {
      // Make sure the database is connected
      $this-&gt;_connection or $this-&gt;connect();
  
      return (bool) $this-&gt;_connection-&gt;query('COMMIT');
  }
  
  /**
   * Rollback a SQL transaction
   *
   * @param string Isolation level
   * @return boolean
   */
  public function rollback()
  {
      // Make sure the database is connected
      $this-&gt;_connection or $this-&gt;connect();
  
      return (bool) $this-&gt;_connection-&gt;query('ROLLBACK');
  }
  
  public function list_tables($like = NULL)
  {
      if (is_string($like))
      {
          // Search for table names
          $result = $this-&gt;query(Database::SELECT, 'SHOW TABLES LIKE '.$this-&gt;quote($like), FALSE);
      }
      else
      {
          // Find all table names
          $result = $this-&gt;query(Database::SELECT, 'SHOW TABLES', FALSE);
      }
  
      $tables = array();
      foreach ($result as $row)
      {
          $tables[] = reset($row);
      }
  
      return $tables;
  }
  
  public function list_columns($table, $like = NULL, $add_prefix = TRUE)
  {
      // Quote the table name
      $table = ($add_prefix === TRUE) ? $this-&gt;quote_table($table) : $table;
  
      if (is_string($like))
      {
          // Search for column names
          $result = $this-&gt;query(Database::SELECT, 'SHOW FULL COLUMNS FROM '.$table.' LIKE '.$this-&gt;quote($like), FALSE);
      }
      else
      {
          // Find all column names
          $result = $this-&gt;query(Database::SELECT, 'SHOW FULL COLUMNS FROM '.$table, FALSE);
      }
  
      $count = 0;
      $columns = array();
      foreach ($result as $row)
      {
          list($type, $length) = $this-&gt;_parse_type($row['Type']);
  
          $column = $this-&gt;datatype($type);
  
          $column['column_name']      = $row['Field'];
          $column['column_default']   = $row['Default'];
          $column['data_type']        = $type;
          $column['is_nullable']      = ($row['Null'] == 'YES');
          $column['ordinal_position'] = ++$count;
  
          switch ($column['type'])
          {
              case 'float':
                  if (isset($length))
                  {
                      list($column['numeric_precision'], $column['numeric_scale']) = explode(',', $length);
                  }
              break;
              case 'int':
                  if (isset($length))
                  {
                      // MySQL attribute
                      $column['display'] = $length;
                  }
              break;
              case 'string':
                  switch ($column['data_type'])
                  {
                      case 'binary':
                      case 'varbinary':
                          $column['character_maximum_length'] = $length;
                      break;
                      case 'char':
                      case 'varchar':
                          $column['character_maximum_length'] = $length;
                      case 'text':
                      case 'tinytext':
                      case 'mediumtext':
                      case 'longtext':
                          $column['collation_name'] = $row['Collation'];
                      break;
                      case 'enum':
                      case 'set':
                          $column['collation_name'] = $row['Collation'];
                          $column['options'] = explode('\',\'', substr($length, 1, -1));
                      break;
                  }
              break;
          }
  
          // MySQL attributes
          $column['comment']      = $row['Comment'];
          $column['extra']        = $row['Extra'];
          $column['key']          = $row['Key'];
          $column['privileges']   = $row['Privileges'];
  
          $columns[$row['Field']] = $column;
      }
  
      return $columns;
  }
  
  public function escape($value)
  {
      // Make sure the database is connected
      $this-&gt;_connection or $this-&gt;connect();
  
      if (($value = $this-&gt;_connection-&gt;real_escape_string((string) $value)) === FALSE)
      {
          throw new Database_Exception('[:code] :error', array(
              ':code' =&gt; $this-&gt;_connection-&gt;errno,
              ':error' =&gt; $this-&gt;_connection-&gt;error,
          ), $this-&gt;_connection-&gt;errno);
      }
  
      // SQL standard is to use single-quotes for all values
      return "'$value'";
  }
  </code></pre>
  
  <p>} // End Database_MySQLi</p>