1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
<?php defined('SYSPATH') or die('No direct script access.');
/**
* MySQL database connection.
*
* $Id: Database_Mysqli.php 4679 2009-11-10 01:45:52Z isaiah $
*
* @package Kohana
* @author Kohana Team
* @copyright (c) 2008-2009 Kohana Team
* @license http://kohanaphp.com/license
*/
define('RUNS_MYSQLND', function_exists('mysqli_fetch_all'));
class Database_Mysqli_Core extends Database_Mysql {
public function connect()
{
if (is_object($this->connection))
return;
extract($this->config['connection']);
// Persistent connections are supported as of PHP 5.3
if (RUNS_MYSQLND AND $this->config['persistent'] === TRUE)
{
$host = 'p:'.$host;
}
$host = isset($host) ? $host : $socket;
if($this->connection = new mysqli($host, $user, $pass, $database, $port)) {
if (isset($this->config['character_set']))
{
// Set the character set
$this->set_charset($this->config['character_set']);
}
// Clear password after successful connect
$this->db_config['connection']['pass'] = NULL;
return $this->connection;
}
// Unable to connect to the database
throw new Database_Exception('#:errno: :error',
array(':error' => $this->connection->connect_error,
':errno' => $this->connection->connect_errno));
}
public function disconnect()
{
return is_object($this->connection) and $this->connection->close();
}
public function set_charset($charset)
{
// Make sure the database is connected
is_object($this->connection) or $this->connect();
if ( ! $this->connection->set_charset($charset))
{
// Unable to set charset
throw new Database_Exception('#:errno: :error',
array(':error' => $this->connection->connect_error,
':errno' => $this->connection->connect_errno));
}
}
public function query_execute($sql)
{
// Make sure the database is connected
is_object($this->connection) or $this->connect();
$result = $this->connection->query($sql);
// Set the last query
$this->last_query = $sql;
return new Database_Mysqli_Result($result, $sql, $this->connection, $this->config['object']);
}
public function escape($value)
{
// Make sure the database is connected
is_object($this->connection) or $this->connect();
return $this->connection->real_escape_string($value);
}
} // End Database_MySQLi
|