Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

Session variables

Codes here !

Moderators: macek, egami, gesf

Session variables

Postby fourptoh » Tue Mar 20, 2012 3:28 pm

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
Attachments
img2.png
img2.png (2.92 KiB) Viewed 321 times
img1.png
img1.png (10.84 KiB) Viewed 321 times
fourptoh
New php-forum User
New php-forum User
 
Posts: 20
Joined: Wed Jan 11, 2012 7:53 am

Re: Session variables

Postby freshnet » Thu Mar 22, 2012 2:29 pm

You're double quoting your variables, which is causing MYSQL to interpret them as strings. Use ' instead (note that you'll then have to use " for your query string e.g.

Code: Select all
$sql = "INSERT INTO sessions (userid, username) VALUES ('$user_id','$user_name')";
User avatar
freshnet
php-forum Active User
php-forum Active User
 
Posts: 278
Joined: Tue Feb 22, 2011 8:19 am
Location: Canada

Re: Session variables

Postby fourptoh » Wed May 02, 2012 11:37 pm

thanks i'll give it a try
fourptoh
New php-forum User
New php-forum User
 
Posts: 20
Joined: Wed Jan 11, 2012 7:53 am


Return to mySQL & php coding

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron