By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,506 Members | 1,876 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,506 IT Pros & Developers. It's quick & easy.

Include Problem

P: n/a
Ken
Hello:

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"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>

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

Thanks,

Ken
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
.oO(Ken)
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?
I guess so. A filesystem access should be faster than a server request.
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>
[...]

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


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
header.

HTH
Micha
Jul 17 '05 #2

P: n/a
Ken
On Sat, 18 Sep 2004 19:56:59 +0200, Michael Fesser <ne*****@gmx.net>
wrote:
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>
[...]


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
header.


Michael,

Thank you very much for your reply and explanation.

Is it odd that it works as a query result?

Ken
Jul 17 '05 #3

P: n/a
Ken wrote:
Hello:

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.


Why don't you try something like this:

class Whatever {
function Whatever() {
...
}

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

function output_head() {
// output common header
...
}

function ouput_body() {
// output custom content
...
}
}

and then for every page you have

$whatever = new Whatever();
$whatever->output();

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) :)

Lüph
Jul 17 '05 #4

P: n/a
.oO(Ken)
Is it odd that it works as a query result?


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.

Micha
Jul 17 '05 #5

P: n/a
Ken
On Mon, 20 Sep 2004 01:04:55 +0200, Michael Fesser <ne*****@gmx.net>
wrote:
.oO(Ken)
Is it odd that it works as a query result?


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.


Micha

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

<?php
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";
exit;
}
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 : " .
mysql_error());

/* HTML Header */
include("HTMLHeader.txt")
include("PageHeader.txt");

etc...

}
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.