0

I am following The book "Beginning PHP, Apache, MySQL web development" by Wrox. I have been following it verbatim and for some reason I am having a issue with the code. The editor says that there are no errors in my code. but when I run it gives me the following message: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 3" here is the following code

<?php
//take in the id of a director and return his/her full name
function get_director() {

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));
$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;
 }
   // conect to MySQL
   $db = mysql_connect('localhost', 'root', 'root') or 
   die ('unable to connect. Check your parameters');

    // make sure you are yousing 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 the release</th>
       <th>Movie Director</th>
       <th>Movie Lead Actor</th>
       <th>Movie Type</th>
     </tr>
     ENDHTML;

      //loop throught 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
  ?>

After that I tried copying and pasting the exact code thinking maybe I did something wrong and here it is the following code:

 <?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
 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', 'root') 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;
 ?> 

when I ran the code this time, the table header shows but part of the code is also displayed on the browser. It looks like this: ENDHTML; // loop through the results while ( = mysql_fetch_assoc(Resource id #3)) { extract(); = get_director(); = get_leadactor(); = get_movietype(); .= << ENDHTML; } .= <<

any help will be appreciated I am new to programming thanks

Luke
  • 22,826
  • 31
  • 110
  • 193
Ed Banes
  • 169
  • 1
  • 1
  • 8
  • 2
    Please, [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). You will also want to [Prevent SQL Injection!](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Nov 24 '14 at 15:53
  • You cannot have any spaces or tabs before the closing identifier of a heredoc. And you should make just one query joining the necessary tables, don't query for additional information in the loop. – jeroen Nov 24 '14 at 15:54
  • Echo your `$query` and you'll see problems with it. More than likely a quote issue on these line(s) `WHERE people_id = ' . $director_id;`. – Jay Blanchard Nov 24 '14 at 15:54
  • `spaces ENDHTML;` as already stated. Remove the spaces before `ENDHTML;` there should not be anything before and after heredocs. [Error reporting](http://php.net/manual/en/function.error-reporting.php) would have given you an error, had it been set. – Funk Forty Niner Nov 24 '14 at 16:02
  • thank you all. So just to be clear No one would use this approach for their site? – Ed Banes Nov 24 '14 at 16:33
  • **Throw this book in the garbage.** It's at *least* ten years out of date and is promoting a lot of dangerously bad habits. Don't even bother reading another page. Modern PHP development involves using a [development framework](http://codegeekz.com/best-php-frameworks-for-developers/) like [Laravel](http://laravel.com/) that fits your style and needs, where you're given far more in the way of tools to work with than low-level PHP calls like `mysql_query`. For more best practices, read [PHP the Right Way](http://www.phptherightway.com/). – tadman Nov 24 '14 at 16:35
  • thanks a lot tadman benn real rough looking for a quality book on site development. Thanks – Ed Banes Nov 24 '14 at 16:44
  • @Jay Blanchard does pdo or mysql work with a mysql database. I don't know anything of the two. And ere these the new wave of doing things? Should I stop learning mysql? is it a waste. Overtime I learn something it seems its being deprecated. Whats the right direction to go in in site development using php and a mysqli database – Ed Banes Nov 24 '14 at 19:42
  • Yes, both work with MySQL. It is the `mysql_*` functions that are deprecated, so don't stop learning MySQL, just learn the right functions to use with it. MySQL is the database, the other items are the things we use to interact with the database. – Jay Blanchard Nov 24 '14 at 19:47

2 Answers2

2

When you end an heredoc you must not put any char at the begining of the line. In your code there are heredocs with spaces or tabs before them. Delete the spaces and put your heredoc at the begining of the line.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Serpes
  • 672
  • 4
  • 14
0

Closing heredoc statement must be at the very first position in the string:

ENDHTML;    // works
  ENDHTML;  // won’t work
Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160