我无法解决有关HEREDOC语法错误的问题 [英] I am unable to fix an issue concerning an HEREDOC syntax error

查看:98
本文介绍了我无法解决有关HEREDOC语法错误的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是为了避免被标记为重复的链接

heredoc和PHP问题



我无法解决开发人员提供的解决方案后的问题。在这段代码中肯定有一个语法错误,但我找不到它的位置,事实上,下面的Heredoc语句不起作用,并且阻止了整个代码的工作,而且如果我尝试运行代码在我的Web服务器上,我有一个500服务器错误。我修改了我的问题,并在其中实现了答案。

在编辑这个问题之前,我试着自己解决这个问题,但我已经到了一个盲区。我刚刚在代码的开头添加了错误报告,即使重新打开旧问题不太正确。


$ b

 <?php error_reporting(E_ALL); ini_set('display_errors',1);?><?php //取一个导演的id并返回他/她的全名name get_director($ director_id){global $ db; $ query ='从people WHERE people_id ='选择people_fullname。 $ DIRECTOR_ID; $ result = mysql_query($ query,$ db)或死(mysql_error($ db)); $ row = mysql_fetch_assoc($ result);提取物($行);返回$ people_fullname;} //获取主角的id并返回他/她的全名name get_leadactor($ leadactor_id){global $ db; $ query ='从people WHERE people_id ='选择people_fullname。 $ leadactor_id; $ result = mysql_query($ query,$ db)或死(mysql_error($ db)); $ row = mysql_fetch_assoc($ result);提取物($行);返回$ people_fullname;} //获取电影类型的ID并返回有意义的文本//描述function get_movietype($ type_id){global $ db; $ query ='选择movietype_label从movietype WHERE movietype_id ='。 $ TYPE_ID; $ result = mysql_query($ query,$ db)或死(mysql_error($ db)); $ row = mysql_fetch_assoc($ result);提取物($行);返回$ movietype_label;} //连接到MySQL $ db = mysql_connect('localhost','root','xxxxxxxx')或die('Unable to connect。Check your connection parameters。'); //确定你是使用正确的databasemysql_select_db('moviesite',$ db)或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)或die(mysql_error($ db)); //确定返回结果中的行数$ num_movies = mysql_num_rows($ result); $ table =<<< ENDHTML< div style =text-align:center;> < h2>电影评论数据库< / h2> < table border =1cellpadding =2cellspacing =2style =width:70%; margin-left:auto; margin-right:auto;> < TR> < th>电影标题< / th>发行年份< / th> < th>电影导演< / th> < th> Movie Lead Actor< / th> < th>电影类型< / th> < / TR> ENDHTML; / *循环遍历结果* / 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> $导演< / TD> < TD> $ leadactor< / TD> < TD> $ movietype< / TD> < / TR> ENDHTML; } $ table。=<<< ENDHTML< / table> < p> $ num_movies电影< / p> < / DIV> ENDHTML; echo $ table; ?>>这是我收到的错误ENDHTML; / *循环遍历结果* / while(= mysql_fetch_assoc(Resource id#3)){extract(); = get_director(); = get_leadactor(); = get_movietype(); 。=<< ENDHTML; }  


解决方案

不要在结束标识符 ENDHTML; 之前留出任何空格,这些空格中明确包含两个空格。





    $ b

    错误报告可能会引发该语法错误。

    >



    阅读heredoc :






    警告
    请务必注意,关闭标识符必须不包含其他字符,例如分号(;)。这意味着特别是标识符可能不会缩进,并且在分号之前或之后可能没有任何空格或制表符。认识到关闭标识符之前的第一个字符必须是由本地操作系统定义的换行符也很重要。这是在UNIX系统上的\ n,包括Mac OS X.结束分隔符后面还必须跟有一个换行符。







    脚注:



    mysql _ * 函数弃用注意:



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



    此扩展从PHP 5.5.0开始不推荐使用,不适用建议编写新的代码,因为它将来会被删除。相反,要么 mysqli PDO_MySQL 扩展名应该被使用。另请参阅 MySQL API概述 。在选择MySQL API时获得进一步的帮助。

    这些函数允许您访问MySQL数据库服务器。有关MySQL的更多信息,请参见» http://www.mysql.com/



    MySQL的文档可以在» http://dev.mysql .com / doc /

    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; }

    解决方案

    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/.

    这篇关于我无法解决有关HEREDOC语法错误的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆