Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Include weiredness 1

Status
Not open for further replies.

rrsub

MIS
Oct 23, 2002
536
US
I just moved a site to another server and for some reason, my includes aren't working on scripts that are more than 1 deep.

I have a file that includes a file (variables). If a condition is met (say session var), another include is called (cleanup).

It's the second include that fails; more specifically, any mysql_query in the second include gets me :

mysql_query(): supplied argument is not a valid MySQL-Link

It's running PHP 4.3.10.

Another thing is that I can't call the second include using relative paths, it has to be absolute.

 
scripts that are more than 1 deep"?

If you're getting the "supplied argument is not a valid MySQL-link" error, then the query your script is passing to MySQL has a problem, and your script doesn't have the necessary code to deal with the problem.

At a minimum, something like:

$dbh = mysql_query ($some_query) or die (mysql_error());

will give you more meaningful information.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
It's my $Link variable (Mysql Connection string)

I have
File.php -> includes config.php
config.php -> $Link is Resource id #4
config.php (if condition) calls -> purge.php
purge.php -> $Link is blank


That wasn't the case on the old server. This worked fine for about 2 years and I'm not quite sure what the difference is on 4.3.10.


 
It would make helping you a lot easier if you did not write in macros.


What is $Link? Is it a variable that holds the output of mysql_connect() or mysql_query()?

When you reference this inside purge.php, is it from within a user-defined function?


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
When config.php is 'included', the proper Database connection is made and $Link is:

Code:
$Link = mysql_connect( $db['host'], $db['username'], $db['password'] );
if (! $Link){die($error1);exit;}

within config.php, there is a session variable which if it isn't set, purge.php is included;

Code:
if(!isset($_SESSION['purge'])) 
{
	#include("purge.php");
	include("[URL unfurl="true"]http://".$_SERVER[/URL]['SERVER_NAME']."/form/inc/purge.php"); 
	$_SESSION['purge']++;
}

of which {include("purge.php");} used to work and now it doesn't which is why there is the absolute path below it.

Within purge.php, is this snippet:

Code:
$i=0;
while($purge_db[$i])
{
	$sql = "DELETE FROM ".$purge_db[$i].$sql_tail;
	$i++;
	mysql_query($sql, $Link) or die (mysql_error());
}


Now when I echo $Link from config.php, $Link is Resource id #4
$Link in purge.php is empty

From what I can tell, the server is:
MACHTYPE i386-redhat-linux-gnu

and is hosted at big mega telecommunications company (which took 3 weeks to get them to install MySql for me)
 
Are config.php and purge.php on the same machine? If so, you must not include the file via a URL, but rather by a filesystem filename. This is because when you include a PHP file via a URL, the script is run in a separate process, which means no variables from the including script will be available to the included script.

If purge.php is on a different machine, you cannot have it use the MySQL connection handle that is created in config.php, as purge.php will be running on a different machine in a different process space.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Thanks for that tip. Will change to [document_root].

Still, any reason why my includes don't work?

 
Reread my last post.


Keep in mind that PHP's filesystem functions are not constrained to the document root, so include may require a pathname that is relative to your entire filesystem.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
That's what I'm saying. This site worked for about 2 years on one server using relative paths. Now on this server, I only get 1 include deep.

The only thing I see off hand that is different is the [include_path] which on the older server is: .:/usr/local/apache2/php/lib/php
The current (new) server is .:../PEAR-1.1

 
This:

include("['SERVER_NAME']."/form/inc/purge.php");

Is not a relative path. This is a URL inclusion.

This:

#include("purge.php");

is a commented-out filesystem inclusion, but if purge.php is not in the same directory as config.php, your include_path would have to be set just so.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Here's my current snippet:

Code:
($_SERVER['DOCUMENT_ROOT']."/form/inc/db.php");

And this workaround works and I appreciate your sharing of knowledge and experience.

What I'm puzzled by is why I have to change my code this way. This site works on Windows IIS servers, RH9, FC1, FC2, with Apache 1.3.x, 2.0.4x, and 2.0.5x with PHP 4.2.x up to 4.3.<10.

Another tidbit is this server is with a mega communication company that I (and their client) had to bark our way up the ladder to get MySQL installed which was promised at the getgo months ago.

Basically the workaround is to redeclare the database connection in every 'include' file which I've done and I'm not happy with it because I don't quite get why my connection drops after nested includes.
At least the Sessions hold.

 
To the best of my knowledge, you should not have to reconned to MySQL in every include file, as included files inherit the variable scope of the including script.

I intuit, though, that you have multiple PHP configuration differences that are causing a lot of your problems.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Those were test configurations so this is really one site on one server that is in the process of being moved.

Again, the main thing I noticed is that I call my config.php which has the Database conection ($Link).
In the first include file, $Link obviously has a value. It's when the first include file includes another that ($Link) loses it's value which is why I have to 'include' the database connection in every include file.


 
You're right -- given what you've stated, it's a strange behavior that should not be happening.

However, I don't know enough more about your code to be able do advise you, and I can think of at least one thing which might affect your MySQL connectivity: variable scoping inside user-defined functions in your include files.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Sorry, clicked "submit" I meant "preview".

Generally, what I do in my code is to do something like:

$GLOBALS['mysql_connector'] = mysql_connect(....);

if ($GLOBALS['mysql_connector'] == FALSE)
{
//error
}
else
{
.
.
.
}

Then anywhere my script needs to talk to MySQL, I invoke mysql_query with an explicit use of the connection resource:

$rh = mysql_query ($query, $GLOBALS['mysql_connector'])...;

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top