I am currently creating a very basic webpage to test a database. I am very, very new at web development, so bear with me. I am attempting to embed php into the html in order to dynamically populate a drop-down menu with a list of universities from a database. However, looking at the source code once I upload it to the server, the embedded php code is being read in-correctly... it is interpreting ">" within a if statement I have to be ending the php, rather than waiting for "?>". How do I fix this?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en"> 
 <head> 
  <title>Search</title> 
 </head> 
 <h>Search for your university
 <body> 
    <?php
    //connection to db stuff

    $sql        = "SELECT campus FROM universities";  
    $query      = mysql_query($sql); 
    $options    = "";
    if (mysql_num_rows($query) > 0)  //php code cuts off at ">" and simply prints the rest
    {
        while($row = mysql_fetch_array($query)) 
        {
            $options .= "<option>" . $row['campus'] . "</option>";
        }
    }
?>
//form stuff
 </body> 
</html>

Comments

It could be a stupid question, but how did you saved the page? As stated by others, your php is not being read by the server, and that '>' is interpreted as html tag and the browser gets confused and prints the rest (but not what comes before). It's either your php install is not configured or your page is a simple html page

Written by Damien Pirsy

Accepted Answer

You can also try the following code and save it as a .php (for example index.php) file

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en"> 
  <head> 
    <title>Search</title> 
  </head>
  <body>
  <h1>Search for your university</h1>

  <?php
  //connection to db stuff

  $sql        = "SELECT campus FROM universities";  
  $query      = mysql_query($sql); 
  $options    = "";
  if (mysql_num_rows($query) > 0)  //php code cuts off at ">" and simply prints the rest
  {
    while($row = mysql_fetch_array($query)) 
    {
        $options .= "<option>" . $row['campus'] . "</option>";
    }
    // Show the dynamicly created options
    echo $options;
  }
  ?>
  //form stuff
  </body> 
</html>

I added a fix for your BODY tag. It was added on the wrong place, and a fix for the tag. That one whas incomplete.

It is not necesary to save a file as .php onto a server to parse it as PHP, but it is recommended and it is easier to understand for other programmers.

Written by Bearwulf
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki