I'm very new to drupal, and I am far from a php expert, but I think this would be very mysterious for most people.

I have two simple PHP scripts that print a DB Table to the screen, paginate, and sort it via a mysql query. In order to call this program from a drupal node I "encapsulate" the "root level" php in a function named after the file. I then call it from drupal (or from a test file) like this:


<?php require_once('MyScript.php'); MyScript.php; ?>

... to me this seems intuitive enough, but no so. These "sister scripts" are very similar, and both fail early on, when different functions in each return NULL. This is very odd to me because those same functions work fine when I call them from OUTSIDE of drupal.

Situation One

<?php
function ListingManager()
	{
		require_once "HTML/Template/IT.php";
		require_once 'local/myFunctions.php';
	
		$_SESSION = doSetup(); //Sets $_SESSION and $_GET variables for pagination, entries per page, sort order, etc.
		
		mysql_connect("localhost", "root", "password") or die(mysql_error());
		mysql_select_db("DB") or die(mysql_error());
		
		$query = "SELECT * FROM inventory";
		$query .= " ORDER BY " . $_SESSION[Sort] . " " . $_SESSION[Order];
		$query .= " LIMIT " . ($_SESSION[PageNumber] - 1) * $_SESSION[EntriesPerPage] . ", " . $_SESSION[EntriesPerPage] . ";";

		$sql = mysql_query($query);

//RESULT OF MYSQL_QUERY:

//Outside Drupal Node:  Returns 'Resource id# 8"

//From within drupal:  Returns NULL
?>

Then, a few lines later, when i try to mysql_fetch_array(), I error fatally.

I also have a very similar app which is basically not part of the same program, but only because the table it needs to print to the screen is so different. The interesting thing is that it fails relative to the first one on the NEXT line, for the same reason.

<?php
function InventoryManager()
	{
		require_once "HTML/Template/IT.php";
		require_once 'local/myFunctions.php';
	
		$_SESSION = doSetup(); //Sets $_SESSION and $_GET variables for pagination, entries per page, sort order, etc.
		
		mysql_connect("localhost", "root", "password") or die(mysql_error());
		mysql_select_db("DB2") or die(mysql_error());

                $query = "SELECT DISTINCT(style), vendor, color, retail FROM stock";
		$query .= " ORDER BY " . $_SESSION[Sort] . " " . $_SESSION[Order];
		$query .= " LIMIT " . ($_SESSION[PageNumber] - 1) * $_SESSION[EntriesPerPage] . ", " . $_SESSION[EntriesPerPage] . ";";
	
		$sql = mysql_query($query);
		
		$template = new HTML_Template_IT("./templates");

//RESULT OF ATTEMPTING TO CREATE TEMPLATE OBJECT:

//Outside of Drupal:  $template is an instance of the HTML_Template_IT object.

//Inside of Drupal:  $template is NULL.
?>

Incomprehensibly, this means that mysql_query worked in this one, but the next line fails. This program then completes, but prints nothing onto the screen, as the template is obviously not loaded.

Below is the function doSetup(); that sets the $_SESSION and $_GET variables as described earlier...


<?php
	session_start();

	function doSetup()
	{
		if ($_GET[np] == 1)
		{
			unset($_SESSION[Sort]);
		}
		if ($_GET[Sort])
		{
			$_SESSION[Sort] = $_GET[Sort];
		}
		else
		{
			if (! $_SESSION[Sort])
			{
				$_SESSION[Sort] = "style";
			}
		}
		if ($_GET[EntriesPerPage])
		{
			$_SESSION[EntriesPerPage] = $_GET[EntriesPerPage];
		}
		else
		{
			$_SESSION[EntriesPerPage] = "50";
		}
		if ($_GET[PageNumber])
		{
			$_SESSION[PageNumber] = $_GET[PageNumber];
		}
		else
		{
			$_SESSION[PageNumber] = "1";
		}
		if ($_SESSION[Sort] == $_GET[Sort])
		{
			if ($_SESSION[Order] != "DESC")
			{
				$_SESSION[Order] = "DESC";
			}
			else
			{
				$_SESSION[Order] = "ASC";
			}
		}
		return ($_SESSION);
	}

	function InventoryManager()
	{

//This is already the function I posted earlier
?>

...as far as I can tell, this should be ok, but what do I know?

So I'm pretty much out of ideas, besides learning the drupal module paradigm, which I'm not sure if I'm quite ready for yet. Its hard enough for me to understand my own code a few weeks after i wrote it :)

Any help is much appreciated. Feel free to find me on irc in #drupal=support (and sometimes #drupal) where ive been idling since this came up.

Comments

nevets’s picture

On the call to session_start(), not needed since it is already called by drupal and may cause problems.

On the database calls, you should read "How to connect to multiple databases within Drupal" in the handbook.

On paths (includes and call to HTML_Template_IT()), all yours paths are relative to the current URL which depending on where you are including your script and is likely to produce unexpected results. For example if your node you include the script in has a node id of 243 the URL will end with node/243 and PHP will expect to find the files relative to a directory called node/243 which is off the root of your site. Since this directory does not exist the includes will fail.