开发者

PHP Code after '>' Displayed as HTML

I am going nuts trying to use PHP to get and insert values into an SQLite database. I haven't even managed to get that far yet!

It seems that all the php code after '>' is displayed as html text and I cant understand why. The code is posted below.

   <html>
<head></head>
<body>
<?php
try
{
  //create or open the database
  $database = new SQLiteDatabase('myDatabase.sqlite', 0666, $error);
}
catch(Exception $e)
{
  die($error);
}

//add Movie table to database
$query = 'CREATE TABLE Movies ' .
         '(Title TEXT, Director TEXT, Year INTEGER)';

if(!$database->queryExec($query, $error))
{
  die($error);
}

//insert data into database
$query =
  'INSERT INTO Movies (Title, Director, Year) ' .
  'VALUES ("The Dark Knight", "Christopher Nolan", 2008); ' .

  'INSERT INTO Movies (Title, Director, Year) ' .
  'VALUES ("Cloverfield", "Matt Reeves", 2008); ' .

  'INSERT INTO Movies (Title, Director, YEAR) ' .
  'VALUES ("Beverly Hills Chihuahua", "Raja Gosnell", 2008)';

if(!$database->queryExec($query, $error))
{
  die($error);
}

//read data from database
$query = "SELECT * FROM Movies";
if($result = $database->query($query, SQLITE_BOTH, $error))
{
  while($row = $result->fetch())
  {
    print("Title: {$row['Title']} <br />" .
          "Director: {$row['Director']} <br />".
          "Year: {$row['Year']} <br /><br />");
      }
    }
    else
    {
      die($error);
    }
    ?>
    </body>
    </html>

And this is what is displayed in html.

queryExec($query, $error)) { die($error); } //insert data into database $query = 'INSERT INTO Movies (Title, Director, Year) ' . 'VALUES ("The Dark Knight", "Christopher Nolan", 2008); ' . 'INSERT INTO Movies (Title, Director, Year) ' . 'VALUES ("Cloverfield", "Matt Reeves", 2008); ' . 'INSERT INTO Movies (Title, Director, YEAR) ' . 'VALUES ("Beverly Hills Chihuahua", "Raja Gosnell", 2008)'; if(!$database->queryExec($query, $error)) { die($error); } //read data from database $query = "SELECT * FROM Movies"; if($result = $database->query($query, S开发者_JAVA百科QLITE_BOTH, $error)) { while($row = $result->fetch()) { print("Title: {$row['Title']}" . "Director: {$row['Director']}". "Year: {$row['Year']}"); } } else { die($error); } ?> 

I am running this on Mac OS X on my localhost apache server.

Any help would be awesome.


since code between <?php and -> contains no < or > symbols it's treated like some kind of html tag. so, you see everything outside if this tag. this means - php isn't installed (properly)

try this: check your httpd.conf for such string: AddType application/x-httpd-php .php and try to save your file as .php


If you're using a .html file, try renaming it to .php.


Are you sure your php installation is working? Try running this script (test.php):

<?php
phpinfo();
?>


if you are saying that code after ?> running as html then it is completely fine..because ?> is closing tag for php ..

anything under "" is considered as php and outside of it consider as html...

or php server is not installed properly so that everything is considered as html code..


Surely your Apache is not configured properly with php, its just sending the raw page, not only after >, check view-source in your browser you can see all your code


What filename have you used for the script?


Your code is not executed on the server. Apache delivers your php-Sourcecode. Your webbrowser thinks that <?php is an opening tag, but it does not recognize it and does not display the tag. Here your browser thinks that the tag I mentioned ends: if(!$database->queryExec($query, $error)). See the > in this line? So your browser believes that everything from <?php to this > is an unknown tag, which it ignores and does not display. In your Browser right click on the page and select view source code. Then you should see the full source.

Make sure that you have mod-php correctly installed in your apache or that you have your cgi-mod setup correctly. Check your httpd.conf for the registered file extensions (should look like AddType application/x-httpd-php .php .phtml .php3). Make sure that your php-file has a file existensions that fits to this configuration.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜