2

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, SQLITE_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.

6
  • 1
    you installed php also, or just trying from apache? Commented Jul 5, 2011 at 11:15
  • giv the apache and php version Commented Jul 5, 2011 at 11:16
  • 1
    Hang on, what line of the code posted does everything start being outputted as HTML? Commented Jul 5, 2011 at 11:16
  • I have trouble understanding your question. Which '>' do you mean? Is ALL php source code displayed (which would mean that your apache is misconfigured) or just a certain part? Commented Jul 5, 2011 at 11:16
  • Sorry should have specified. I have put in what is output in my html browser Commented Jul 5, 2011 at 11:18

7 Answers 7

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

I can't find that line in my httpd.config?
I added it and it made no difference
tried stripping the php code out and putting it in a separate file called test.php and running it with 'php test.php' but still to no avail. It must be something to do with the php setup on my mac
1

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

Comments

0

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

<?php
phpinfo();
?>

Comments

0

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

1 Comment

I have editedd my question to show what is being output in my html browser. This code is basically copied from a tutorial located at switchonthecode.com/tutorials/…
0

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

2 Comments

I just check my source and up until that '>' its all in pink which i guess is the colour for php. Its as if its reading it as a closing bracket?
Its reading it as html tag, so its not displaying that, but your server is not at all executing your php code.
0

What filename have you used for the script?

2 Comments

for the above code I have it in an html file called test.html and access it using 127.0.0.1/test.html
Rename the file to test.php then.
0

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.

1 Comment

Ok managed to get this working and it displays the phpinfo(). What I did was re-copy my php.ini.default file to php.ini. So basically started over.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.