Db
ATTENTION! This library is soon-to-be replaced by pdo
.
All database queries are routed through the db library. Along with providing some useful methods, db ensures that queries are protected from SQL injections and that the system is database provider agnostic.
Properties
id
The current database session id.
connect()
Connects to the database using the specified parameters. Sets this connection as the default.
$this->zajlib->db->connect($server="", $user="", $pass="", $db="", $fatal_error = true)
@param string
$server The hostname or ip of the server.
@param string
$user The mysql user name.
@param string
$pass The mysql password.
@param string
$db The name of the database to use for this connection
@param boolean
$fatal_error If set to true (the default) a failed connection will result in a fatal error.
@return boolean
Returns true if successful, false (or fatal error) otherwise.
create_connection()
Same as connect, but does not set this as the default. This is only needed if you need to connect to a separate database. If you need seperate queries, use sessions.
$this->zajlib->db->create_connection($server="", $user="", $pass="", $db="", $id = false)
@param string
$server The hostname or ip of the server.
@param string
$user The mysql user name.
@param string
$pass The mysql password.
@param string
$db The name of the database to use for this connection
@param bool|string
$id The id does not need to be specified, but you can choose any string if you wish.
@return zajlib_db_session
A new session will be returned.
query()
Execute a the specified SQL query on the database.
@param string $sql
The SQL query to execute.
@param boolean $disable_error
If set to true, no error will be returned or logged, though false will still be returned if the query failed.
@return zajlib_db_session
Returns the current query session.
If you just want to run a single SQL statement:
$result = $this->zajlib->db->query('SELECT column_name FROM some_table WHERE id=123"); $row = $result->next(); print $row->column_name;
You can also foreach through the results:
$result = $this->zajlib->db->query('SELECT column_name FROM some_table"); foreach($results as $row){ print $row->column_name; }
The above statements will use the default MySQL connection specified in site/index.php
. If you need to connect to another database you can do this like so:
$dbsession = $this->zajlib->db->create_connection($server="", $user="", $pass="", $db="", $id = false); $results = $dbsession->query('SELECT column_name FROM some_table"); foreach($results as $row){ print $row->column_name; }