Php/mysql database connection problem

Hi folks.
I have a php/mysql problem I hope you can help with.
I am trying to connect to a database to pass some values to a mysql database. The problem sits with my function making a connection.

This may have something to do with global variables but I can’t seem to make it work.

Here is the code for the funtion…


function insert_handicap_event( ) {
	include_once("Connections/conn.php");		
	mysql_select_db($database_conn, $conn);
	$query = "CALL insert_handicap_event('$event_date', '$league)";
	$do_query = mysql_query($query, $conn) or die(mysql_error());
	}

here is conn.php


$hostname_conn = "localhost";
$database_conn = "arb";
$username_conn = "root";
$password_conn = "";
$conn = mysql_pconnect($hostname_conn, $username_conn, $password_conn) or trigger_error(mysql_error(),E_USER_ERROR); 

I am getting the following errors echoed out to the screen.

Notice: Undefined variable: database_conn in C:\wamp\www\functions.php on line 164

Notice: Undefined variable: conn in C:\wamp\www\functions.php on line 164

Warning: mysql_select_db() expects parameter 2 to be resource, null given in C:\wamp\www\functions.php on line 164

Notice: Undefined variable: arb_conn in C:\wamp\www\functions.php on line 166

Warning: mysql_query() expects parameter 2 to be resource, null given in C:\wamp\www\functions.php on line 166

Can anyone shed any light on what I should try please?

Thanks in advance.

Your function does not know about the variables as they are declared outside of it. The term is “variable scope”.

Read the corresponding manual page carefully, and come back if you cannot work out how to get round it.

Thanks for the tip regarding scope. I got round it using the $GLOBALS[‘variable’] method. Is this safe enough? I always heard it was dangerous to use global variables or is that something else I’m getting confused with?

Anyway - thanks again!