▼  Site Navigation Main Articles News Search  ▼  Anime + Manga Anime Reviews Anime Characters Gallery Screenshots Manga Reviews  ▼  Misc Links to Webcomics Bible Quotes About Older Musings
site version 7.3
PHP –– include
written by: admin


Date Written: 10/10/08 Last Updated: 9/4/10

Absolute vs. relative includes
files can be included either absolutely or relatively.

Absolute include():
include 'http://www.mysite.com/include/quote.php';

Relative include()
include '../include/quote.php';
include 'quote.php';


When using an absolute include the file will be treated as an external file even though the file may be located on your website.

Example 1

test1.php:
<?php
$ID=3;
include 'http://www.mysite.php/test2.php';
echo "$ID";
?>

test2.php:
<?php
$ID=4;
?>

If you run test1.php The result will be 3 not 4.

If test2.php had
<?php
print_r($_COOKIE);
?>


The result would be Array() if you tried to run test.php.
Example 2
To include the variable you need to use a relative include as in the following example:
test1.php:
<?php
$ID=3;
include 'test2.php';
echo "$ID";
?>

test2.php:
<?php
$ID=4;
?>

If you run test1.php The result will now be 4.


database connections
If your included file connects to the database the connection is valid only in the included file.  The database connection does not persist to the included file either.  To illustrate this take a look at the following examples:

Example 1

test1.php
<?php
include 'connect.php';
include 'http://www.mysite.com/test2.php';
   $get_addresses = "select ID, summary from table where ID = 55";
   $get_addresses_res = mysql_query($get_addresses);
   if (mysql_num_rows($get_addresses_res) > 0) {
$add_info = mysql_fetch_array($get_addresses_res);
$summary  = $add_info['summary'];
}
echo"$summary";
?>

test2.php
<?php
   $get_addresses = "select ID, summary from table where ID = 55";
   $get_addresses_res = mysql_query($get_addresses);

   if (mysql_num_rows($get_addresses_res) > 0) {
$add_info = mysql_fetch_array($get_addresses_res);
$summary  = $add_info['summary'];
}
echo"$summary";
?>
Here you will get an error message and then your data, because the database connection does not apply into the included file: test2.php.

Example 2

connect.php
<?php
   $get_addresses = "select ID, summary from table where ID = 55";
   $get_addresses_res = mysql_query($get_addresses);

   if (mysql_num_rows($get_addresses_res) > 0) {
$add_info = mysql_fetch_array($get_addresses_res);
$summary  = $add_info['summary'];
}
echo"$summary";
?>

test2.php
<?php
include 'connect.php';
   $get_addresses = "select ID, summary from table where ID = 55";
   $get_addresses_res = mysql_query($get_addresses);

   if (mysql_num_rows($get_addresses_res) > 0) {
$add_info = mysql_fetch_array($get_addresses_res);
$summary  = $add_info['summary'];
}
echo"$summary";
?>
Here you will get the data and then an error message because the database connection applies only to the include file: test2.php.

NOTE: If you are unable to include data absolutely you will need to edit your php.ini permissions.  To do that you will need to edit your php.ini file and turn the allow_url_fopen on.

TAGS: php
copyright 2005–2024