解析错误:语法错误,php文件中意外的文件结束

时间:2014-07-14 14:42:38

标签: php

你能否告诉我这段代码的结尾有什么问题?为什么它会说:“解析错误:语法错误,php文件中意外的文件结束”:

<?php

define("DB_SERVER" , "localhost");
define("DB_USER" , "root");
define("DB_PASS" , "root");
define("DB_NAME" , "widget_corp");
?>
<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <title> CMS MANAGEMENT </title>
    <link rel="stylesheet" type="text/css" href="stylesheets/style.css">
</head>
<body>
    <header>
    <h1> widget Corp <h1>
    </header>
<?php
require("constants.php");


// mysql connection
    $connection = mysqli_connect(DB_SERVER,DB_USER,DB_PASS);
    if (!$connection){
        die("Database Connection failed:". mysql_error());
    }

// select database
    $db_select = mysqli_select_db($connection , DB_NAME);
    if (!$db_select) {
        die("Database selection failed:" . mysql_error());
        }
    ?>
    <nav>
    <ul class="subject">
<?php 

    $subject_set = mysqli_query($connection, "SELECT * FROM subjects" );
    if (!$subject_set){
        die("mysql failed" . mysql_error());
    }
        while ($subject = mysql_fetch_array($subject_set)){
        echo "<li>{$subject["menu_name"]}</li>";
    }
        $page_set = mysqli_query($connection, "SELECT * FROM pages WHERE subject_id = {$subject["id"]}");
            if (!$page_set){
                die("Database query failed: " . mysql_error);


        echo "<ul class=\"pages\">";
        while ($page = mysqli_fetch_array($page_set)){
            echo "<li>{$page["menu_name"]}</li>";
    }
    echo "</ul>";


?>
    <?php echo "</ul>"; ?>

    </section>

    <?php mysqli_close($connection);?>

1 个答案:

答案 0 :(得分:1)

修改

我还为格式化更新了一些代码,但您错过了</nav>标记。另外,如果您不在</ul>块的中间,则回显php标记是没有意义的。


您缺少括号}。我之后添加了它:

if (!$page_set){
                die("Database query failed: " . mysql_error);

<?php require_once("includes/functions.php"); ?>
<?php require_once("includes/constants.php"); ?>
<?php include("includes/header.php"); ?>
<?php include("includes/connection.php"); ?>
    <nav>
        <ul class="subject">
            <?php 
                $subject_set = mysqli_query($connection, "SELECT * FROM subjects" );
                if (!$subject_set){
                    die("mysql failed" . mysql_error());
                }

                while ($subject = mysql_fetch_array($subject_set)){
                    echo "<li>{$subject["menu_name"]}</li>";
                }

                $page_set = mysqli_query($connection, "SELECT * FROM pages WHERE subject_id = {$subject["id"]}");

                if (!$page_set){
                    die("Database query failed: " . mysql_error);
                }

                echo "<ul class=\"pages\">";
                while ($page = mysqli_fetch_array($page_set)){
                    echo "<li>{$page["menu_name"]}</li>";
                }
                echo "</ul>";
            ?>
        </ul>
    </nav>
</section>

<?php mysqli_close($connection);?>