Php 无法显示数据库中的文本

Php 无法显示数据库中的文本,php,database,Php,Database,我的网站上有一个新闻源。您可以在post.php网站上发布新闻,当您发布新闻时,新闻就会出现在this news.php上。代码如下所示: <!DOCTYPE html> <html lang='en'> <head> <style type="text/css"> element { color: black; } body { -webkit-background-size: cover; -moz-background-size: cov

我的网站上有一个新闻源。您可以在post.php网站上发布新闻,当您发布新闻时,新闻就会出现在this news.php上。代码如下所示:

<!DOCTYPE html>
<html lang='en'>
<head>
<style type="text/css">
element {
color: black;
}


body {
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-attachment: fixed;
background-image: url(bgn1.png);
background-repeat: no-repeat;
background-position: center top;
 }
.news {
font-family: Rough_Typewriter;
font-size: 36px;
 }
 </style>

 <meta charset="UTF-8">
 <title>iWrite</title>
 <meta name="view" content="width-device-width, initial-scale=1.0">
 <meta name="description" content="">
 <meta name="keywords" content="">

 <link href="css/bootstrap.css" rel="stylesheet">
 </head>

 <h1 class="news">iWrite</h1>
 <p>
 <hr>
  <p>
 <p>
    <p>
    <p>
  <p>
   <p>
  <p>
  <p>
   <p>
  <a href='post.php'>Want to post a text?</a>
  <?php

   //connect

   mysql_connect("myserver","username","password") or die(mysql_error());
    mysql_select_db("databasename") or die(mysql_error());

    //query the database
    $getnews = mysql_query("SELECT * FROM news ORDER BY id DESC") or die(mysql_query());

     while ($row = mysql_fetch_assoc($getnews))
     {

//get data
$id = $row['id'];
$title = $row['title'];
$body = $row['body'];
$date = $row['date'];

echo "
<b>$title posted on $date</b><br>
";

echo nl2br($body);

echo "<hr>
";

    }



    ?>
但当它在index.html网站上显示它时,它只是回显在$date上发布的$title。。。。
请帮帮我

如果要嵌入要解析的PHP代码,必须使用PHP文件扩展名。如果您需要使用.html,请尝试在.htaccess中使用mod_rewrite。

您不应该在PHP中使用类似的变量。 别让他们插手

echo "<b>". $title ." posted on ". $date ."</b><br />"
此外: 使用或死亡是不好的做法,因为这会扼杀脚本。
使用正确的错误处理。

他正在这样做:«这个新闻出现在这个news.php»他说当前的文件名是news.php!检查他的问题的最后一行:但当它显示在index.html网站上时,它只是回显$date上发布的$title。。。。请帮帮我!是的,我也看到了。对不起@David,你是在index.html还是news.php上这么做的?试着把它保存为index.php。你的答案也是错误的。不要将连接运算符与echo一起使用,请使用逗号。i、 e.echo,$title,'posted on',$date@迈克,我可以问一下为什么这被认为是一种不好的做法吗?是否存在此漏洞?@itachi-如果使用连接运算符,PHP将创建一个新的临时字符串变量,将所有内容连接到该变量中,然后回显临时变量。如果您所做的只是回显,则无需执行任何这些操作,因此只需使用逗号立即回显值。