host_name = $host_name;
$this->user_name = $user_name;
$this->db_name = $db_name;
// Make the connection to the server..
$this->conn = new \mysqli( $host_name, $user_name, $pass, $db_name );
if ( !$this->conn || ($this->conn->connect_errno > 0) )
{
die( sprintf( 'Could not connect to "%s" with "%s; error follows:
%s"', $host_name, $user_name, $this->conn->connect_error ) );
}
}
/**
* Closes the connection opened by the constructor..
*/
public function __destruct()
{
// Close up the connection, if it exists..
if ( $this->conn != null )
{
$this->conn->close();
}
}
/**
*
* @param type $query
* @return db_recordset The recordset from the query
*/
public function run_query( $query )
{
// Run that query and stop if there is an error..
$result = $this->conn->query( $query );
if ( !$result )
{
die( sprintf( 'There was an error running the query: %s
Here is the error: %s', $query, $this->conn->error ) );
}
// Otherwise, making it here means we can wrap/return the result..
return new db_recordset( $result );
}
/* PRIVATE */
private $conn = null;
private $host_name = null;
private $user_name = null;
private $db_name = null;
}
/**
* Wraps a mysqli_result and acts mostly as a pass-through to hide
* the underlying implementation should the recommended PHP method
* of database interaction change again - note: this class should
* really be used alongside 'select' queries, not 'update', 'delete',
* or 'insert'..
*/
class db_recordset
{
/* PUBLIC */
/**
* Constructs a new result..
* @param \mysqli_result $result
*/
public function __construct( $result )
{
$this->result = $result;
}
/**
* Frees the result when out of scope..
*/
public function __destruct()
{
if ( ( $this->result != null ) && ( is_object( $this->result ) ) )
{
$this->result->free();
}
}
/**
* Retrieves the number of records in this recordset..
* @return type
*/
public function num_recs()
{
// If the query was something like a 'select' statement,
// then the result is a recordset and the nmber of records
// is stored in that recordset object..
if ( is_object( $this->result ) )
{
return $this->result->num_rows;
}
// Otherwise, if the query was something like an 'update'
// statement, then the result IS the number of affected recs..
return $this->result;
}
/**
* Enumerates the next record in this recordset..
* @param boolean $as_obj
* Set to true if you would like records to be returned as objects
* instead of an associative array; defaults to the array
* @return mixed Could be an object or associative array based on initial setting
*/
public function next( $as_obj = false )
{
// Do nothing if the result is not a recordset..
if ( !is_object( $this->result ) )
{
return null;
}
// As an object..
if ( $as_obj )
{
return $this->result->fetch_object();
}
// e.g. {[field1]=>value1,[field2]=>value2,[fieldX]=>valueX}..
return $this->result->fetch_assoc();
}
/**
* Gets at the underlying mysqli_result for direct access;
* for example, could use this to call \mysqli::prepare..
* @return \mysqli_result
*/
public function get_mysqli_result()
{
return $this->result;
}
/* PRIVATE */
private $result = null;
}
?>