Page 1 of 1

Undefined variable

Posted: Sat Sep 10, 2011 2:45 pm
by Elbryan
I'm running into a problem where a file that I'm including that defines a variable is not being found. I am using this variable in a similar context for another file and it's finding it properly. Here's the flow:

Working version:
Uses Ajax to ping GetBrowseData.php which includes Connection.php. Connection.php has a variable $CardTable defined and GetBrowseData.php uses that correctly.

GetBrowseData.php

Code: Select all

<?php
include("Enums.php");
include("Connection.php");

$sqlConnection = OpenConnection();
mysql_query( $CardTable, $sqlConnection ) or mysql_error();  //  Works, $CardTable is valid
...
?>
Connection.php

Code: Select all

<?php
$CardTableName = "MyTableName";
$CardTable = "CREATE TABLE ".$CardTableName." (
    rowID int NOT NULL AUTO_INCREMENT,
    PRIMARY KEY(rowID),
    Name varchar(30)
)";

function OpenConnection()
{
  $sqlConnection = mysql_connect( blah blah );  //  Removed from this example for obvious reasons
  if( !$sqlConnection )
  {
    die( 'Could not connect: ' . mysql_error() );
  }
  mysql_select_db( blah blah );
  return $sqlConnection;
}
 
?>
Now for the problem. My CreateState.php file includes CreateCard.php. This file is hit via a form in CreateState.php that does the following:

Code: Select all

<form id="Data" action = "CreateState.php?userEntry=writeData" method = "post">
Then I respond to this by calling CreateCard() from my CreateState.php file. CreateCard.php includes Connection.php which *should* give me access to $CardTable, but instead gives me an "Undefined variable" notice. I'm at a loss as to why. Any ideas? Here's the relevant code:

CreateState.php

Code: Select all

<html>
<body onload="OnLoad();">

<?php
	include( "Enums.php" );
	include( "Header.php" );
	include( "CreateCard.php" );
?>

...

<?php 
//  User selected the CreateCard button
if( isset($_GET['userEntry']) && $_GET['userEntry'] == "writeData" )
{
	CreateCard();
}
//  New user
if( !isset($_GET['userEntry']) )
{
?>
	<form id="Data" action = "CreateState.php?userEntry=writeData" method = "post">
	...
	//  Above form calls it's own file with the writeData when they press submit.
Pressing submit then sends the user back to the above file and calls CreateCard below.

CreateCard.php

Code: Select all

<?php
include( "Enums.php" );
include( "Connection.php" );

function CreateCard()
{
  echo $CardTable; //  Fails, gives me Notice: Undefined variable.
  $sqlConnection = OpenConnection();
}
Enums.php simply defines arrays such as $CardName, $CardColor, etc that are shared by many different files. There is no accidental duplication of $CardTable inside this file.

Does anyone know what might cause it to fail to find $CardTable in this instance?

Re: Undefined variable

Posted: Sat Sep 10, 2011 7:51 pm
by nowaydown1
This appears to be a variable scope issue. In the first example you posted, your variable is declared in a global scope, and you use the variable in a global scope also. In the second example you gave, where it is not working for you, you are trying to access a variable that is in the global scope from a functional scope. No variable with that name exists inside the function scope, therefore you get the notice. To resolve this, you would need to declare the variable as a global:

Code: Select all

include( "Enums.php" );
include( "Connection.php" );

function CreateCard()
{
   global $CardTable;

  echo $CardTable; //  Fails, gives me Notice: Undefined variable.
  $sqlConnection = OpenConnection();
}
This will work but is generally considered a bad way to go. The better solution would be to move your connection include up a level, then pass in your $CardTable variable as a function parameter. Hope that helps.