Include Problem

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I'm writing my first php app and I have a problem.
Initially I put template type strings, such as HTML Headers and page
footers in a MySQL table and called them up with a php SQL query to
include them in the printed HTML, This worked great but then I learned
about includes and I tried using includes to put them into the HTML.

1. I assume that this is faster than the query method because that's
what people seem to do.  Am I right about that?

2. The following string works as a query result but not as an include
file.  Could someone be so kind as to explain to me why?

<?xml version="1.0" encoding="UTF-8"\x3f>
<!DOCTYPE html
  PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  " ">


The parser chokes on the first line.  If I remove it the include works



Re: Include Problem


Quoted text here. Click to load it

I guess so. A filesystem access should be faster than a server request.

Quoted text here. Click to load it

You have short_open_tags enabled in your php.ini, so the parser treats
the first two chars of <?xml as the beginning of new PHP block, which
causes a parse error. Three solutions:

1) Disable short_open_tags in your php.ini.
2) Use PHP and a print or echo statement to print out the XML prolog:

   <?php print "<?xml version='1.0' ...?>";?>

3) Simply remove the prolog. You only need it if you want to use another
   encoding than UTF-8 (used by default) and no encoding information was
   given in another protocol level, for example in the HTTP response


Re: Include Problem

Quoted text here. Click to load it


Thank you very much for your reply and explanation.

Is it odd that it works as a query result?


Re: Include Problem


Quoted text here. Click to load it

No, because to print out a query result you already use a print or echo
statement, i.e. you are inside a PHP-block. No problem with that. In
your example you try to write it out directly in a HTML-block without
PHP, but PHP interprets it wrong, hence the parse error.


Re: Include Problem


Quoted text here. Click to load it


Please bear with me while I try to grasp this.  My include statement
is inside a php block as far as I can see:

  if (!isset($_SERVER['PHP_AUTH_USER'])) {
   header('WWW-Authenticate: Basic realm="My Realm"');
   header('HTTP/1.0 401 Unauthorized');
   echo "Access to ITR Billing canceled";
  else {
   $userName = $_SERVER['PHP_AUTH_USER'];
   $password = $_SERVER['PHP_AUTH_PW'];
/* Connecting, selecting database */
$link = mysql_connect("localhost", $userName, $password)
   or die("Could not connect : " . mysql_error());
mysql_select_db("ITRBilling") or die("Could not select database");

/* Performing SQL querys */
$query2 = "SELECT userName FROM Users WHERE `userName` = \"$userName\"
AND `password` = \"$password\"" ;
$result2 = mysql_query($query2) or die("Query failed : " .

/* HTML Header */



Re: Include Problem

Ken wrote:
Quoted text here. Click to load it

Why don't you try something like this:

class Whatever {
    function Whatever() {

    function output() {
        echo "<!DOCTYPE ...";
        echo "<html>";
        echo "</html>";

    function output_head() {
        // output common header

    function ouput_body() {
        // output custom content

and then for every page you have

$whatever = new Whatever();

you can pass parameters to the constructor and then use them to further
customize the body output (say, maybe a page id, which determines what
include file contains the body) :)


Site Timeline