newbie error, very strange

hi,

I've got a problem that I cant deal with:

I've got a a simple class in a file browse.inc:

<?php
class Connection
{
public $conn;
public $result;
public $resultMeta = array();
}

?>

the file browse.php looks like this:

<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html401/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-2">
<title>Connection</title>
</head>
<body>
<pre>

<?php

include "browse.inc";
?>

</pre>
</body>
</html>

when I run this file everything is ok.

but when I run this file from a file index.php through a z link:

<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html401/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-2">
<title>Connection</title>
</head>
<body>

<h1>DB programming using MySQL and PHP 5.0</h1>

<p>This is a website created in order to get a credit from a DB
laboratoty.
It displays a table and allows to add, remove and update records from
this table.
What would you like to do??</p>

<pre>
<?php
print "\n <a href=http://localhost\DB\browse.php> Browse table
</a>";
?>

</pre>
</body>
</html>

then I get the error parse error, expecting `T_OLD_FUNCTION' or
`T_FUNCTION' or `T_VAR' or `'}''

in line 4 of file browse.inc.

I dunno what may cause this error, maybe someone of you would like to
help me with this.

Pafcio
Pafcio [ Mo, 29 Januar 2007 19:23 ] [ ID #1611967 ]

Re: newbie error, very strange

Pafcio wrote:
> hi,
>
> I've got a problem that I cant deal with:
>
> I've got a a simple class in a file browse.inc:

You should name your include files so that they end with .php, this for
most web servers aren't configured to treat .inc files as php scripts
and will therefore show them as plain text if someone tries to access
the file directly in a browser, and that would be bad if you would have
your username and login for a database stored in a .inc file.


> <?php
> class Connection
> {
> public $conn;
> public $result;
> public $resultMeta = array();
> }
>
> ?>

Try this instead

<?php

class Connection {
public $conn;
public $result;
public $resultMeta;

Connection() {
$this->resultMeta = array();
}

}

?>


In the same way as in C++ classes you should define variables and then
init them in the constructor and each class needs a constructor. PHP
isn't as strict about this as C++, but doing so you will easier get
things to work.


> the file browse.php looks like this:
> <?php
>
> include "browse.inc";
> ?>

This really don't do much, you should create the class variable too



> <?php
> print "\n <a href=http://localhost\DB\browse.php> Browse table
> </a>";
> ?>

I can notice you are using microsoft, the path you have given isn't
really valid path, should have been

echo "\n <a href=\"http://localhost/DB/browse.php\">Browse table</a>";

you should always put html tag values inside double quotes, this will
save you from a lot trouble, you use the \ as an escape character, which
makes it possible to include double quotes as output inside a double
quote (hope that didn't confuse you).

You can alway use http://validator.w3.org/ to check your html output is
correct.


--

//Aho
Shion [ Di, 30 Januar 2007 00:24 ] [ ID #1611968 ]
PHP » alt.php » newbie error, very strange

Vorheriges Thema: difference between two date in seconds: problem with daylight saving time
Nächstes Thema: Get error when I add new theme & select it in setting. Error shuts down postnuke