Jump to content

Recommended Posts

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 [email protected] to report this error, try again later!");

	echo "    Page Access logged";


	}

?>

Yes, this is a scope problem.  Variables you define outside of a function are not available inside the function.  In this case: $hostname_conn, $username_conn, $password_conn, $database_conn, $pageID, $pageType, $ip, $email are NOT available to the function.  There are two ways to solve this problem.  The easiest way, which is the sloppiest and least recommended, is to make these available inside the function using the global keyword (I'm not even going to show you how).

 

The cleanest way is to pass into the function what you need.  But before we do that, you should reduce the function to a single task.  So take the database connection stuff out and leave it at the global level.

 

$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);

 

then create the function with parameters (use a reference for the db connection NOT a copy)

function insertStats($pageID, $pageType, $ip, $email, &$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 [email protected] to report this error, try again later!");

    echo "    Page Access logged";
}

 

then call the function passing the required parameters

insertStats($pageID, $pageType, $ip, $email, $db_conn);

 

Other points to consider:

or die() is not a nice way to handle an error.  Consider having the function return a value of TRUE or FALSE depending on success and let the calling routine decide what to do when the function fails.

or trigger_error() is just going to output an error message, it will continue to execute the following statements.  You should check the result of the function call and decide how to handle a case when the database is not accessible.

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.