<?php
/**
* mysql.class.php
*
* Contains the MySQL class which adds functionality and act as a DB engine object to the DAo class from the GiMb framework.
* @author Georgi Momchilov
* @version 1.0
* @package GiMb
* @link gmomchilov(at)gmail(dot)com
*/
/**
* MySQL class
*
* @author Georgi Momchilov
* @version 1.0
* @package GiMb
* @link gmomchilov(at)gmail(dot)com
*/
class MySQL extends DAO {
/**
* Constructor
*
* Empty - to avoid extension loop
* @return void
*/
function MySQL(){}
/**
* Connector
*
* Connects to to DB server using mysql api functions
* @param array @server Server to connect to
* @return bool
*/
function connect( $server ){
$this->db_conn = mysql_connect( $server['host'], $server['username'], $server['password'] );
if( mysql_select_db( $server['database'], $this->db_conn ) )
return true;
else
return false;
}
/**
* Get the optimal slave server using MySQL api functions
*
* @return array $server
*/
function getSlave() {
$conns = 9999999;
$slave = false;
$result = false;
foreach( $this->db_servers as $server ) {
if( $server['type'] == 'master' ) continue;
$this->connect( $server );
$result = mysql_query("SHOW STATUS LIKE 'Connections' AS conns");
$c = mysql_fetch_assoc( $result );
$c = $c['conns'];
if( $conns > $c ) {
$conns = $c;
$slave = $$server;
}
}
if( !$slave ) {
$this->_setError('DB servers are overloaded!', $this->db_logfile );
return false;
}
else
return $slave;
}
/**
* Get number of rows in mysql query resource
*
* @return int
*/
function getNumRows(){
$result = mysql_query("SELECT FOUND_ROWS()");
$result = mysql_fetch_row( $result );
return $result[0];
}
/**
* Execute SQL query using MySQL api functions
*
* @return bool
*/
function query( $sql ){
return mysql_query( $sql );
}
/**
* Fetch associative array using MySQL api functions
*
* @return array
*/
function fetchAssoc( $db_result ){
return mysql_fetch_assoc( $db_result );
}
}
?>