0

This is the link in order to avoid to be marked as duplicate

Issue with heredoc and PHP

I haven't been able to solve the issue following the solutions provided by the developers. Within this snippet of code there must be certainly a syntax error but I can't find where it is, as a matter of fact the below Heredoc statement does not work and prevents the whole code from working, furthermore if I try to run the code on my web server I have a 500 server error. I have modified my question implementing the answers into it.

Before editing this question I've tried to solve the problem on my own, but I've gotten to a blind alley. I've just added the error reporting in the beginning of the code, even if it is not very correct to reopen old questions.

   
<?php error_reporting(E_ALL); ini_set('display_errors', 1);?>
<?php
// take in the id of a director and return his/her full name
function get_director($director_id) {
 
    global $db;
 
    $query = 'SELECT 
            people_fullname 
       FROM
           people
       WHERE
           people_id = ' . $director_id;
    $result = mysql_query($query, $db) or die(mysql_error($db));
 
    $row = mysql_fetch_assoc($result);
    extract($row);
 
    return $people_fullname;
}

// take in the id of a lead actor and return his/her full name
function get_leadactor($leadactor_id) {
 
    global $db;
 
    $query = 'SELECT
            people_fullname
        FROM
            people 
        WHERE
            people_id = ' . $leadactor_id;
    $result = mysql_query($query, $db) or die(mysql_error($db));
 
    $row = mysql_fetch_assoc($result);
 extract($row);
 
    return $people_fullname;
}

// take in the id of a movie type and return the meaningful textual
// description

function get_movietype($type_id) {
 
    global $db;
 
    $query = 'SELECT 
            movietype_label
       FROM
           movietype
       WHERE
           movietype_id = ' . $type_id;
    $result = mysql_query($query, $db) or die(mysql_error($db));
 
    $row = mysql_fetch_assoc($result);
    extract($row);
 
    return $movietype_label;
}

//connect to MySQL
$db = mysql_connect('localhost', 'root', 'xxxxxxxx') or 
    die ('Unable to connect. Check your connection parameters.');
// make sure you’re using the right database
mysql_select_db('moviesite', $db) or die(mysql_error($db));
// retrieve information
$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));
// determine number of rows in returned result
$num_movies = mysql_num_rows($result);
 $table = <<<ENDHTML
 <div style="text-align: center;"> 
  <h2>Movie Review Database</h2> 
  <table border="1" cellpadding="2" cellspacing="2" 
  style="width: 70%; margin-left: auto; margin-right: 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; 
   /* loop through the results */
    while ($row = mysql_fetch_assoc($result)) {
    extract($row);
    $director = get_director($movie_director);
    $leadactor = get_leadactor($movie_leadactor);
    $movietype = get_movietype($movie_type);
 $table .= <<<ENDHTML
  <tr>
    <td>$movie_name</td>
    <td>$movie_year</td>
    <td>$director</td>
    <td>$leadactor</td>
    <td>$movietype</td>
  </tr>

  ENDHTML;    
 }
$table.= <<<ENDHTML
    </table>    
  <p>$num_movies Movies</p> 
  </div> 

  ENDHTML;

echo $table;
  
?>

> this is the error that I receive
ENDHTML; /* loop through the results */ while ( = mysql_fetch_assoc(Resource id #3)) { extract(); = get_director(); = get_leadactor(); = get_movietype(); 

.= << ENDHTML; }
Community
  • 1
  • 1
Riccardo
  • 66
  • 2
  • 16
  • 1
    To avoid being makred as a duplicate, you could link to the similar question you found where the answer didn't work for you. – Relequestual May 08 '15 at 15:32
  • 1
    Also, people will need to see what the content of the error is in order to help you =] – Relequestual May 08 '15 at 15:33
  • ` ENDHTML;` looks like 2 spaces before `ENDHTML;` – Mark Baker May 08 '15 at 15:34
  • Why use heredoc at all? Just end the php before the html and start it before the loop – mplungjan May 08 '15 at 15:36
  • You edited your question with "my fix" about the spaces in your heredoc, without marking it as an edit. Please don't do that. People will visit the question and see your new code and my answer and say: *"there's no spaces in there, so why the answer?"* - in turn possibly downvoting my answer for it. I performed a rollback to your original post. – Funk Forty Niner May 08 '15 at 15:56
  • i'm sorry for that fred i try to rewrite the answer? – Riccardo May 08 '15 at 16:05

2 Answers2

1

There should not be any spaces before your closing identifier ENDHTML; which clearly contains 2 spaces for each of them.

  • Remove them.

Error reporting would have caught that syntax error.

Read up on 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.


Footnotes:

mysql_* functions deprecation notice:

http://www.php.net/manual/en/intro.mysql.php

This extension is deprecated as of PHP 5.5.0, and is not recommended for writing new code as it will be removed in the future. Instead, either the mysqli or PDO_MySQL extension should be used. See also the MySQL API Overview for further help while choosing a MySQL API.

These functions allow you to access MySQL database servers. More information about MySQL can be found at » http://www.mysql.com/.

Documentation for MySQL can be found at » http://dev.mysql.com/doc/.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • i have tried to remove the spaces from the code and what I have is: ENDHTML; /* loop through the results */ while ( = mysql_fetch_assoc(Resource id #3)) { extract(); = get_director(); = get_leadactor(); = get_movietype(); .= << ENDHTML; } .= < – Riccardo May 08 '15 at 15:49
  • @Riccardo ok well, you accepted the other answer. That's ok. I guess that 50% and 50% equals a solution. – Funk Forty Niner May 08 '15 at 15:53
  • in the other comment it's written that I must use Heredoc so I accepted both of them because both are solutions to me the only thing is that I must use Heredoc so I've got to find a way to proceed by the way I removed spaces and it still does not work – Riccardo May 08 '15 at 15:57
  • @Riccardo it doesn't matter which one you choose as correct, just as long as you have your solution. But again, please don't edit your question with code that was taken from my answer or any other answer. That's not how it's done here. – Funk Forty Niner May 08 '15 at 15:59
  • Can you update your question with an UPDATE: and the code that did not work (which cannot be posted in a comment) while leaving the first part of the code intact? – mplungjan May 08 '15 at 15:59
  • i will try to do this way and fred how can I solve my problem without using the code in your asnwer? you said remove the spaces and I did it that's it – Riccardo May 08 '15 at 16:02
  • @Riccardo I have gone over your code over and over again, and cannot see why it's failing. You are able to use `mysql_` functions, correct? You have another piece of code that you can confirm that does work with `mysql_*` functions? – Funk Forty Niner May 08 '15 at 16:22
  • @Riccardo you're also no obligation to accept my answer, since it doesn't solve the question completely. You can un-accept if you wish, but just don't go re-accepting the other answer also. – Funk Forty Niner May 08 '15 at 16:24
  • @Riccardo Add error reporting to the top of your file(s) right after your opening PHP tag for example ` – Funk Forty Niner May 08 '15 at 16:39
  • @Fred-ii- I've just tried to add the error reporting and it works, I've a long list of errors, but I am unable to find a solution, because even if there are errors I can't fix them, do you need that I show you the error page? – Riccardo May 12 '15 at 08:26
1

How about

$num_movies = mysql_num_rows($result);
?><div style="text-align: center;"> 
  <h2>Movie Review Database</h2> 
  <table border="1" cellpadding="2" cellspacing="2" 
  style="width: 70%; margin-left: auto; margin-right: 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>
<?php   /* loop through the results */
    while ($row = mysql_fetch_assoc($result)) {
      extract($row);
      $director = get_director($movie_director);
      $leadactor = get_leadactor($movie_leadactor);
      $movietype = get_movietype($movie_type);
      echo "<tr><td>$movie_name</td><td>$movie_year</td><td>$director</td><td>$leadactor</td><td>$movietype</td></tr>";
    }
    echo "</table><p>$num_movies Movies</p></div>"; 
?>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • I have tried to follow your asswer and it works, but I need to use Heredoc because the book I'm studying use them – Riccardo May 08 '15 at 15:52
  • we'll call this a joint venture then ;-) – Funk Forty Niner May 08 '15 at 15:54
  • Ah, ok, If the books says so then by all means :) But if you used Fred's version plase accept his answer – mplungjan May 08 '15 at 15:55
  • @mplungjan that's ok. OP can choose whichever. I upvoted yours. But OP made an edit in their question without marking it as an edit and I had to perform a rollback to it, in case I get downvoted for it. As per my comment to them under their question. *Cheers* and thanks. – Funk Forty Niner May 08 '15 at 15:57
  • I need to use Heredoc so mplungjan please read the comments cocerning the other answer even if your answer is correct – Riccardo May 08 '15 at 15:58