I am simply trying to insert data into a db. The db connection strings are fine (included file) but when I wrap the code in a function and call it, it fails. It works if I don't wrap the code in a function. Is this a scope problem or something similar?
<?php
if (!isset($_SESSION)) {
session_start();
}
//open connection to database
include("Connections/conn.php");
//register some session variables
$_SESSION['pageID'] = 1;
$_SESSION['pageType'] = "text";
//assign session variables to page variables
$email = $_SESSION['MM_Username'];
$pageID = $_SESSION['pageID'];
$pageType = $_SESSION['pageType'];
$ip= $_SERVER['REMOTE_ADDR'];
insertStats();// this will not successfully run the code in the function below
function insertStats(){
$db_conn = mysql_pconnect($hostname_conn, $username_conn, $password_conn) or trigger_error( mysql_error(),E_USER_ERROR);
//select the database to use
mysql_select_db($database_conn,$db_conn);
//create the SQL statement
$stmnt = "INSERT INTO stats values('','$pageID','$pageType','$ip','$email',now())";
//execute the sql statement
$execute = mysql_query($stmnt,$db_conn)or die ("Error,please email info@xxxx.com to report this error, try again later!");
echo " Page Access logged";
}
?>