开发者

use file_put_contents creat html file with php mysql

When I tried to use file_put_contents creat html file with php mysql, I met some trouble. my code as below, some wrong occured in the first echo line. Need some help. thanks.

The error I get is

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

In this line

$php = "echo '<div class=\"title\">'.htmlspecialchars($row['title'],UTF-8).'</div><br />';

Here is the rest of my code

header('Content-type:text/html; charset=utf-8');
set_time_limit(30);
require_once dirname(__FILE__) . '/conn.php'; 
mysql_select_db("ba_10978243",$handle_db5);
$result = @mysql_query("SELECT title,content,date FROM new_heelp Order By date DESC LIMIT 15");
while ($row = @mysql_fetch_array($result))
{  
$php = "echo '<div class=\"title\">'.htmlspecialchars($row['title'],UTF-8).'</div><br />';//echo wrong in this line : Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING
    $data = date(\"w j n Y H:i:s\", $row['date']);
    echo '<div class=\"date\">'.$data.'</div>';
    if(!empty($row['content'])){ 
      echo '<div class=\"content\">'.$row['content'].'</div>';
    }";
}
file_put_contents("$article_20100421_132.h开发者_运维技巧tml", $php);
?>


That is some rather ugly code. And you are creating strings containing PHP syntax to then be stored within a HTML file? Not going to work.

<?php
set_time_limit( 30 );

require_once( dirname( __FILE__ ) . '/conn.php' ); 
mysql_select_db( 'ba_10978243' , $handle_db5 );

$result = @mysql_query( 'SELECT `title , `content` , `date` FROM `new_heelp` ORDER BY `date` DESC LIMIT 15' );

if( !$result || mysql_num_rows( $result )>0 ){
  echo 'ERROR: No SQL Rows Returned.';
}else{
  $output = array();
  $rowTpl = '<div class="title">%s</div><br/><div class="date">%s</div>%s';
  while( $r = @mysql_fetch_array( $result ) ){
    $output[] = sprintf( $rowTpl ,
                  htmlspecialchars( $row['title'] , ENT_COMPAT , 'UTF-8' ) ,
                  date( 'w j n Y H:i:s' , $row['date'] ) ,
                  ( !empty( $row['content'] ) ? $row['content'] : '' ) );
  }
  file_put_contents( "{$article}_20100421_132.html" , implode( "\n" , $output ) ); # NOTE: $article is not declared anywhere above this line
?>

A summary of issues in your code:

  • You use header() to set the Content-Type when it is not necessary - the HTML file you are creating will not inherit this information, and if this PHP script is not outputting anything to the browser, it is redundant.
  • Double check the Table Name in your SQL - is it really "heelp"?
  • Within the while() loop, you are replacing the value of $php on every loop, so only the last values will be carried down the page.
  • Your string which you are using to set $php contains PHP code, which, in an HTML file, will not do anything.
  • You are escaping double quotes, when surrounded by single quotes - Redundant
  • You are using a variable in the name of the new HTML file which is not declared anywhere above that line, which will cause you problems.


Lots of problems in your code. The most glaring ones are listed below.

"echo '<div class=\"title\">'.htmlspecialchars($row['title'],UTF-8).'</div><br />';
----------------------------^

You're opening your statement with a double quote " but closing it with a single quote '. Just close it with a double quote.

$php = "e....";

You keep overriding the value of the $php variable in the loop. What you want is to concatenate it

$php .= "...";

You've suppressed errors on your query command. How will you know if something goes wrong? Use the @ operator judiciously.

@mysql_query....

From what I see, you're making a mess of your string because you're trying to put PHP code into a text file (maybe to include later or eval it?). There are cleaner ways to craft your string. Makes it much easier to read as well. We can help you if you give more details as to what you're trying to do.


To fix:

 Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

Change your sample line to include a final closing quote:

 $php = "echo '<div class=\"title\">'.htmlspecialchars($row['title'],UTF-8).'</div><br />'";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜