File: example.php

Recommend this page to a friend!
  Classes of Bilge Hauser  >  PHP MySQL Caching Wrapper  >  example.php  >  Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Example usage
Class: PHP MySQL Caching Wrapper
Execute MySQL queries and cache results in files
Author: By
Last change: Added Event Handling examples
Date: 9 years ago
Size: 2,619 bytes
 

 

Contents

Class file image Download
<?php
/*
 * @project: phpMysqlCachingWrapper
 * @date: 04-12-2010
 * @version: 0.1 - php 4
 * @author Bilge Hauser
 * @email: cianti@cianti.de
 * @copyright: (c) 2010 Bilge Hauser
 *
 */

$SITE_CONF = array();
$SITE_CONF['SITEROOT'] = dirname ($_SERVER['SCRIPT_FILENAME']) . '/';
$SITE_CONF['DB_CACHE_PATH'] = $SITE_CONF['SITEROOT'] . 'cache/';
$SITE_CONF['CLASS_PATH'] = $SITE_CONF['SITEROOT'];
$SITE_CONF['DB_HOST'] = 'localhost';
$SITE_CONF['DB_DATABASE'] = 'database';
$SITE_CONF['DB_USER'] = 'user';
$SITE_CONF['DB_PASS'] = 'password';
$SITE_CONF['DB_PERSISTENT'] = false;
$SITE_CONF['DB_USE_SOCKET'] = false;
$SITE_CONF['DB_SOCKET_PATH'] = '/var/lib/mysql/mysql.sock';
$SITE_CONF['DB_USE_CACHE'] = true;
$SITE_CONF['DB_CACHE_TTL'] = 360; //Seconds
$SITE_CONF['DEBUG'] = true;
$SITE_CONF['DEBUG_LOGPATH'] = $SITE_CONF['SITEROOT'] . 'log/phpMysqlCachingWrapper.log';

require_once(
$SITE_CONF['CLASS_PATH'] . 'phpMysqlCachingWrapper.class.php');



$dbh = new mysql_wrapper($SITE_CONF);
// Result comes as asc. array
$countries = $dbh->get_rows('SELECT * FROM land ORDER BY title ASC;');

# $dbh->query('delete from land where land_id = 42');
# $row = $dbh->get_row('select * from land where land_id = 66');
# echo $row['title'];


# Add event handlers
# function to be executed in class using $self
$onBeforeCacheRead = "\$self->setMessageQue('Injected Text Before Cache Read');";
$onAfterCacheRead = "\$self->setMessageQue('Injected Text After Cache Read');";

# set functions to events
$dbh->register_event('CACHE_READ_BEFORE', $onBeforeCacheRead);
$dbh->register_event('CACHE_READ_AFTER', $onAfterCacheRead);

# Send mail if database is not conected
ini_set('sendmail_from', 'me@domain.com');
$onDBConnectError = "mail('cianti@cianti.de', \$_SERVER['SERVER_NAME'].': Database connection error', 'Warning: Database could no be conected on '.\$_SERVER['SERVER_NAME']);";
$dbh->register_event('CLASS_WRAPPER_CONNECT_ERROR', $onDBConnectError);
# Send mail if database querie returns error
$onDBQueryError = "mail('cianti@cianti.de', \$_SERVER['SERVER_NAME'].': Database query error', 'Warning: Database queries had an error on '.\$_SERVER['SERVER_NAME']);";
$dbh->register_event('CLASS_WRAPPER_QUERY_ERROR', $onDBQueryError);



echo
"<html>";
echo
"<body>";

if (
$dhh->getError()) echo "<p><b>".$dhh->getError()."</b></p>";

if (
$SITE_CONF['DEBUG']) echo implode('<br>', $dbh->getMessageQue());

echo
"<p>";
foreach(
$countries as $country){
    echo
$country['title'].'<br>';
}
echo
"</p>";
echo
"</body>";
echo
"</html>";

?>


For more information send a message to info at phpclasses dot org.