database class:
<?php
class database{
  
	var $dbh;
	
  function connect(){
	  
	  $hostname = "";
	  $database = "";
	  $username = "";
	  $password = "";	  	
	  	    
	  try { 
	    $this->dbh = new PDO("mysql:host=".$hostname.";dbname=".$database.";",$username, $password);   
    }
		catch(PDOException $e){
			
    	echo $e->getMessage();    	
    } 
  }
  
  function dbselect($sql){
	  
    $result = $this->dbh->query($sql);
    if($result->rowCount() <= 1)
    {
      $result = $result->fetch(PDO::FETCH_ASSOC);
    }
    elseif($result->rowCount() > 1)
    {
    	$result = $result->fetchall(PDO::FETCH_ASSOC);
  	}
    return $result;
  }
   
  function dbquery($sql){
	  
	  $result = $this->dbh->exec($sql);
  }
  
}
?>
class.php:
<?php
class messaging{
	var $smilies = array( 
	':)'    =>     "smile.gif", 
	':('    =>     "sad.gif", 
	':D'    =>     "biggrin.gif", 
	':d'    =>     "biggrin.gif",                 
	':p'    =>     "tongue.gif", 
	':P'    =>     "tongue.gif", 
	':-)'    =>     "unsure.gif", 
	'(A)'    =>     "angel.gif", 
	'(a)'    =>     "angel.gif", 
	':s'    =>     "blink.gif", 
	':S'    =>     "blink.gif", 
	':$'    =>     "blush.gif", 
	'(h)'    =>     "cool.gif", 
	'(H)'    =>     "cool.gif", 
	':\'('    =>     "cry.gif", 
	'--'    =>     "dry.gif", 
	'-_-'    =>     "dry.gif", 
	'^^'    =>     "happy.gif", 
	'^_^'    =>     "happy.gif", 
	':|'    =>     "huh.gif", 
	';d'    =>     "laugh.gif", 
	';D'    =>     "laugh.gif", 
	':@'    =>     "mad.gif", 
	':o'    =>     "ohmy.gif", 
	':O'    =>     "ohmy.gif", 
	':0'    =>     "ohmy.gif", 
	'8S'    =>     "woot.gif", 
	'8s'    =>     "woot.gif", 
	'8)'    =>     "wacko.gif", 
	';)'    =>     "wink.gif", 
	);  
	
	function bb_code($check) { // maakt ubb codes
	
		foreach($this->smilies AS $find => $replace) 
    {
    	$check = str_replace(htmlentities($find), "<img src='images/smileys/".$replace."' alt='smileys' />", $check); // voegt smileys toe    
  	}
  	$check = preg_replace("#\[img\](http(s)?://)([a-zA-Z0-9\-\.,\?!%\*_\#:;~\\&$@\/=\+]+)\[/img\]#", "<img src='\\1\\3' />", $check); // maakt plaatje
  	$check = preg_replace("#\[color=(\#[0-9A-F]{6}|[a-z\-]+)\](.*?)\[/color\]#si", "<font color='\\1'>\\2</font>", $check); // maakt gekleurde letters
  	
  	return $check;
	}
	function check_message($check){ // bericht beveiligen
	
		$check = mysql_real_escape_string($check);
		$check = bb_code($check);
    $check = nl2br($check);
    return $check;
    
	}
	
	function check_subject($check){ // titel beveiligen
	
		$check = mysql_real_escape_string($check);
    return $check;
    
	}
	
	function send($from,$to,$subject,$message){ // bericht verzenden
	
		$message = $this->check_message($message);
		$subject = $this->check_subject($subject);
				
		$db->dbquery("INSERT INTO pm (msg_from,msg_to,subject,time,message) VALUES (".$from.",".$to.",'".$subject."',NOW(),'".$message."')");
		
	}
}
?>
#####
#SQL#
#####

CREATE TABLE `pm` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `msg_from` int(11) NOT NULL,
  `msg_to` int(11) NOT NULL,
  `subject` text NOT NULL,
  `message` text NOT NULL,
  `time` datetime NOT NULL,
  UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

Members tabel moet minimaal een `id` en een `username` bevatten. 
Ook moet er een $_SESSION['username'] aanwezig zijn.

CREATE TABLE `members` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` text NOT NULL,
  UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

#################
##Voorbeeld scriptjes:##
#################

<?php
# In onderstaande scriptjes gebruik ik de database class.
# Deze kun je op de volgende mabier aanroepen:
# $db = database;
# $db->connect();
?>

Bericht verzenden:
<?php
$pm_class = new messaging; #class aanroepen

$from = 1; #verzender
$to = 10; #ontvanger
$subject = "Mijn onderwerp"; #onderwerp
$message = "Mijn bericht"; #bericht

$pm_class->send($from,$to,$subject,$message);#bericht verzenden
?>

inbox:
<?php
$pm_class = new messaging; #class aanroepen

$bericht = $db->dbselect("SELECT * FROM pm AS p INNER JOIN members AS m WHERE m.username = '".$_SESSION['username']."' AND p.msg_to = m.id"); #inbox
foreach($bericht AS $bericht)
{
	echo "Van:".$bericht->msg_from."<br/>";
	echo "Onderwerp:".htmlspecialchars($bericht->subject)."<br/>";
}
?>

verzonden berichten:
<?php
$pm_class = new messaging; #class aanroepen

$bericht = $db->dbselect("SELECT * FROM pm AS p INNER JOIN members AS m WHERE m.username = '".$_SESSION['username']."' AND p.msg_from = m.id"); #inbox
foreach($bericht AS $bericht)
{
	echo "Van:".$bericht->msg_from."<br/>";
	echo "Onderwerp:".htmlspecialchars($bericht->subject)."<br/>";
}
?>

bericht lezen:
<?php
$pm_class = new messaging; #class aanroepen

$id = 6; # bericht id
$bericht = $db->dbselect("SELECT * FROM pm AS p INNER JOIN members AS m WHERE m.username = '".$_SESSION['username']."' AND p.msg_to = m.id AND p.id = ".$id);; #bericht ophalen
foreach($bericht AS $bericht)
{
	echo "Van:".$bericht->msg_from."<br/>";
	echo "Aan:".$bericht->msg_to."<br/>";
	echo "Onderwerp:".$bericht->subject."<br/>";
	echo "Bericht:".$bericht->message."<br/><hr/>";
}
?>