Hear is the code that captures joomla session variables and tries to insert into a mysql database:
<?php
// Prevents directly accessing the file by typing the path to the file in the URL
// http://forum.joomla.org/viewtopic.php?p=1369600
define( '_JEXEC', 1 );
// In order to use Joomla classes you must first initialize Joomla framework.
// http://www.itjungles.com/other/joomla-login-session
define('JPATH_BASE', dirname(__FILE__) );
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
require('libraries/joomla/factory.php');
// initialize the application
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
// Returns a reference to the global user object, only creating it if it doesn't already exist.
// The object returned will be of type JUser.
// http://docs.joomla.org/JFactory/getUser
// Take in user id parameter to only show for a specific user
$user =& JFactory::getUser();
$user_id = $user->get('id');
$user_name = $user->get('name');
$_SESSION['greeting'] = 'Welcome back' . ' ' . $user . ' ' . $user_name . '<br>';
//$_SESSION['greeting'] = 'Welcome back' . ' ' . $user . '<br>';
$greeting = $_SESSION['greeting'];
echo $greeting . ' ' . 'Your id is: ' . ' ' .$user_id;
$dbh = form_db_connect();
// define INSERT
$sql = '
INSERT INTO sessions (userid, username)
VALUES ("$user_id","$user_name")';
// trap errors
try {
// // prepare and execute INSERT
$sth = $dbh->prepare($sql);
$sth->execute();
// load columns to php variables Populates the php variables
$i=0;
while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
$user_id[$i] = $row['userid'];
$user_name[$i] = $row['username'];
$i++;
}
}
catch (PDOException $e) {
print $e->getMessage();
}
function form_db_connect()
{
try
{
# connect to MySQL server - specify host, database name, username, and password
$dbh = new PDO("mysql:host=localhost;dbname=xxxxxx", "yyyyyy", "zzzzzz-");
# enable exceptions for PDO calls; allows catching errors with try/catch
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e)
{
# Exception error message displayed
echo $e->getMessage();
# way to log errors to a file; LOOK into THIS and implement (?)
# file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
}
return ($dbh);
}
?>this results in the following errors - see attached images.
Can anyone clue me in as to what i'm doing wrong


