rtgx

Pripojenie k databáze

Jednoduchá trieda na pripojenie k databáze so zalogovaním chybného výsledku pripojenia do textového reťazca.

class tx_rtgpayment_functions {

// Database conection
var $db;
var $dbUser = 'username';
var $dbPassword = 'password';
var $dbHost = 'localhost';
var $dbDatabase = 'databasename';
var $log = '';

function init() {

// Connect to database, log connecting errors result
$this->db = @mysql_connect( $this->dbHost, $this->dbUser, $this->dbPassword );
if( !$this->db ) {
$this->log .= date( 'd.m.Y H:i:s' ).'; Unable to connect to DB ('.mysql_error().')'."\n";
exit;
}
if( !@mysql_select_db( $this->dbDatabase ) ) {
$this->log .= date( 'd.m.Y H:i:s' ).'; Unable to select mydbname ('.mysql_error().')'."\n";
exit;
}

}
}

Operácie s tabuľkami

Funkcia rows prenesie výsledky databázového dotazu do poľa pre neskoršie použitie:

	/**
* Create array from database results
*
* @param string $res: DB result
* @return array return: fetch array
*/
function rows( &$res ) {

if( $res ) {
$rows = array();
while( $row = mysql_fetch_assoc( $res ) )
$rows[] = $row;
return $rows;
}
else
return false;
}

Výber záznamov z tabuľky:

	/**
* Select orders
*
* @param string $where: select condition
* @return array return: objects array or false
*/
function getOrders( $where = '', $hidden = 0 ) {

$query = '
SELECT tx_rtgpaymet_orders.uid, FROM_UNIXTIME( tx_rtgpaymet_orders.crdate, \'%d.%m.%Y %H:%i:%s\' ) AS f_crdate
FROM tx_rtgpaymet_orders
WHERE tx_rtgpaymet_orders.deleted = 0 AND tx_rtgpaymet_orders.hidden = '.$hidden.' '.$where.'
ORDER BY tx_rtgpaymet_orders.endtime
';

$res = @mysql_query( $query );
return $this->rows( $res );
}

Úprava záznamu v databáze identifikovaného parametrom uid. Funkcia vráti počet upravených riadkov:

	/**
* Update FE user
*
* @param int $uid: FE user uid
* @param string $groups: FE usergroups
* @return int return: DB result - affected rows
*/
function updateFeuser( $uid, $groups ) {

$query = '
UPDATE fe_users
SET usergroup = \''.trim( $groups ).'\', tstamp = '.time().'
WHERE uid = '.intval( $uid ).'
';

$res = @mysql_query( $query );
return mysql_affected_rows();
}
 
Žiadne dokumenty ani odkazy k tejto stránke.