Include statement in php just displaying code.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
katchoo
Forum Newbie
Posts: 10
Joined: Mon Aug 11, 2003 6:49 pm

Include statement in php just displaying code.

Post by katchoo »

Hi. I have searched so many forums and I am hoping someone here can help me. *begs*

Problem: I have a php script that is the beginnings of a guestbook form. The script is from mysql/php database applications book. In the script they have an include file to create database connections. Great idea! Only that is where my problems start. Instead of connecting I get the code displayed. I know the connection code works because I can place it directly into the script and it does connect.

Script:
<?php
include("dbconnect.php");
echo "Hi <BR>";
$var = Date("H");
if ($var<=11)
{
echo "good morning<BR>";
}
elseif ($var >11 and $var < 18 )
{
echo "good afternoon<BR>";
}
else
{
echo"good evening<BR>";
}
echo "connected";
echo "mysql is closed";
?>

DBconnect.php:
mysql_connect ("localhost", "webuser", "clippers") or die("Could not connect to database");
mysql_select_db ("guestbook") or die ("Could not select database");

Result:

mysql_connect ("localhost", "webuser", "clippers") or die("Could not connect to database"); mysql_select_db ("guestbook") or die ("Could not select database");Hi
good afternoon
connectedmysql is closed

So it just displays what is in dbconnect.php. Which is the source of my frustration.

PHP Configuration :
include_path ./ ./


Please, I would appreciate any ideas you may have. I am new to this, and don't really know anyone to ask for help. My setup is Windows 2000 machine, mysql, php 4.3.2, Apache 2.0.47. Thanks *hugs*
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Does the included file start with:

<?php

.. and end with:

?>
User avatar
katchoo
Forum Newbie
Posts: 10
Joined: Mon Aug 11, 2003 6:49 pm

Post by katchoo »

no it doesn't. I will try that now. The example in the book didn't show it any differently than how i have it.

Thanks ^_^

OMG that worked!!! So whenever I have an include I should place the
"<?php ?> " ? I tried this when my include_path was wrong, but I fixed that and never tried it again.

thank you so much! *hugs*
macewan
Forum Commoner
Posts: 97
Joined: Mon Jul 07, 2003 8:27 pm

Post by macewan »

hugs?
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Hugs are fine. I remember very well how frustrating it is to get stuck on things like this.

The "include()" fn doesn't assume that the file is php, and basically jumps out of php, then jumps back in, in the next line. So if you include a php script it has to be inside php tags.

If you include an html file, it is output direct to the browser - no tags needed.

The html file can of course echo php vars with: <?php echo $var; ?> (jump in and out of php to echo vars declared in the script which made the include call). You can embed php in html or html in php as it suits you.

The above is a common way to keep the programming separate from the page formatting - and then there's also template engines such as smarty http://smarty.php.net/ which may have some advantages. There was a thread in the advanced forum about that recently - sorry forget where exactly.
Post Reply