简体   繁体   中英

I did as it is from the book but i couldnt get rid of the error

i got an error on line 18. You can see there is no space after <<<ENDHTML but i couldn't get rid of error. I even thought I needed to put <?php?> to cover the whole code. It didn't make it work either.

<?php
    $db= mysql_connect('localhost','root','') or die('Unable to connect. Check your connect parameters');

    $query = 'CREATE DATABASE IF NOT EXISTS moviesite';
    mysql_query($query,$db) or die(mysql_error($db));
    mysql_select_db('moviesite', $db) or die(mysql_error($db));

    $query= 'SELECT movie_name, movie_year, movie_director,movie_leadactor,movie_type
             FROM movie
             ORDER BY movie_name ASC, movie_year DESC
             ';
    $result = mysql_query($query, $db) or die(mysql_error($db));

    $num_movies = mysql_num_rows($result);


//needs to show the same result as table1
    $table = <<<ENDHTML<div style="text-align: center;">
        <h2>Movie Review Database</h2>
        <table border="0" cellspacing="2" cellpadding="2" style="width:60%; margin:auto;">
            <tr>
                <th>Movie Title</th>
                <th>Year of Release</th>
                <th>MOvie Director</th>
                <th>Movie Lead Actor</th>
                <th>Movie Type</th>
            </tr>
            ENDHTML;

                while($row = mysql_fetch_array($result)){
                        extract($row);
                        $table .= <<<ENDHTML<tr>
                        <td>$movie_name</td>
                        <td>$movie_year</td>
                        <td>$movie_director</td>
                        <td>$movie_leadactor</td>
                        <td>$movie_type</td>
                        </tr>
                        ENDHTML;
                }
                 $table .=<<<ENDHTML</table>
                <p>$num_movies Movies</p>
                ENDHTML;
                </div>
            ?>


        <p><?php echo $num_movies?>Movies</p>

Firstly, you need to put the code following the identifiers in a new line under it.

$table = <<<ENDHTML<div style="text-align: center;">

such as:

$table = <<<ENDHTML
<div style="text-align: center;">

and

$table .= <<<ENDHTML<tr>

to

$table .= <<<ENDHTML
<tr>

and do the same for all the other ones.

Then, you see these?

</tr>
            ENDHTML;

and

            <p>$num_movies Movies</p>
            ENDHTML;

they contain spaces before your closing identifiers. There shouldn't be any.

  • Remove them

Read the manual http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

Warning

It is very important to note that the line with the closing identifier must contain no other characters, except a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon. It's also important to realize that the first character before the closing identifier must be a newline as defined by the local operating system. This is \\n on UNIX systems, including Mac OS X. The closing delimiter must also be followed by a newline.

Error reporting would have spotted that and would have thrown an:

Parse error: syntax error, unexpected end of file...

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Error reporting should only be done in staging, and never production.

Plus, drop using mysql_ functions. They're deprecated and will be removed from future PHP releases.

Use mysqli with prepared statements , or PDO with prepared statements . They're much safer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM