首先,我想道歉,我还是初学者。
出于学习目的,我正在创建一个博客引擎,我只是注意到,当我列出注释时,回车符(按回车键)等转义字符会正确显示在数据库中,但在显示注释时只显示空白字符。
我正在使用PostgreSQL 8.3。
在这里您可以看到一个示例数据库条目:
fema=> select * from comments where id = 54;
-[ RECORD 1 ]-------------------------
id | 54
authorid | 1
text | This new line won't show.\r
: No\r
: \r
: new\r
: \r
: \r
: \r
: lines.
time | 1341417673
postid | 15
answerid | 0
在这里你可以看到var_dump()显示的内容:
string(50) "This new line won't show. No new lines."
这就是我获取数据的方式:
$stmt = db::$db->prepare("select comments.id as commentid ,authorid,text,time,postid,answerid,users.* from comments left join users on users.id = comments.authorId where postid = :postId");
$stmt->execute(array('postId' => $_GET['p']));
$commentRslt = $stmt->fetchAll();
然后foreach迭代它们并替换我正在使用的标记来识别我必须替换的东西:
$currComment = str_replace('{{{cms:comment:text}}}', $commentRslt[$key]['text'], $currComment);
这是我将新评论插入数据库的方式:
$stmt = self::$db->prepare('INSERT INTO comments (authorId, text, time, postId, answerId) VALUES (:authorId, :text, :time, :postId, :answerId)');
$stmt->execute(array( 'authorId' => $_SESSION['userId'],
'text' => str_replace(array('<','>'), array('<','>'), isset($_POST['newCommentArea']) ? $_POST['newCommentArea'] : $_SESSION['newCommentArea']),
'time' => time(),
'postId' => isset($_POST['commentNew']) ? $_POST['commentNew' ] : $_SESSION['postId'],
'answerId' => $answerId));
很抱歉有很多代码示例,但我甚至不知道问题出在哪里,我想彻底。
所以有人可以告诉我如何解决这个问题吗?如果只是告诉我在哪里弄错了。我真的不知道。
感谢。