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/xhtml开发者_如何学编程1/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>
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.
Simple, your file extension is probably .html
or otherwise.
Rename your file to have a .php
extension, so the PHP interpreter knows what's going on. Otherwise your web server will serve the page as though all contents are html.
What it actually sounds like is that your PHP interpreter isn't touching the code at all... If you view-source, you'll see the code.
The <?php
is being seen as the start of an HTML tag, ending with the >
in the if
conditional.
You need to correct your PHP installation so that it works when you have a test .php file like this:
<?php phpinfo(); ?>
This should return a big descriptive page with all the info on your PHP install. Until you can make that work, you need to start troubleshooting why your PHP install is not working.
Such a problem would indicate a hug eproblem with your install of PHP. it should only ever terminate a PHP block when it encounters a ?>
. If >
would do it, you wouldn't be able to do "greater than" comparisons.
Try removing some things, such as the if()
portion and leave only the while loop, see if that changes anything.
view source, you'll see all you php code.
the php is not running at all in the server, check your config.
Reading your story I understand you load the page onto a server. So I suspect your PHP handler works well. However to show your PHP output on the page, you need to echo the $options variable. Try the following after the while loop
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;
}
精彩评论