无法显示带有SQL表的帖子标题

时间:2011-08-24 01:01:37

标签: mysql sql database-connection

我正在尝试在修改/删除页面中显示标题列表。它们加载得很好,因为我得到了正确的行数,因为某些原因我不能打印文本。

这是与问题相对应的功能。

function manage_content()
{
    echo '<div id="manage">';
    $sql = "SELECT * FROM content";
    $res = mysql_query($sql) or die(mysql_error());
    while($row = mysql_fetch_assoc($res)): ?>
        <div>
            <h2 class="title"><?php $row['title'] ?></h2>
            <span class="actions"><a href="#">Edit</a> | <a href="#">Delete</a></span>
    </div>  <?php
    endwhile;
    echo '</div>'; //End of Manage Div

}

这是完整的代码:

<?php

class blog {
    private $host;
    private $username;
    private $password;
    private $db;
    private $link;

    public function __construct($host, $username, $password, $db){
        $this->db = $db;
        $this->link = mysql_connect($host, $username, $password, $db);
        mysql_select_db($this->db, $this->link) or die (mysql_error());
    }


    function get_content($id=''){
    if($id !=NULL):
        $id = mysql_real_escape_string($id);
        $sql = "SELECT * FROM content WHERE id = '$id'";
        $return = '<p><a href="index.php">Vover al Indice</a></p>';
    else:
        $sql = "SELECT * FROM content ORDER by id DESC";
    endif;

    $res = mysql_query($sql) or die (mysql_error());

    if(mysql_num_rows($res) !=NULL):
    while($row = mysql_fetch_assoc($res)){
        echo '<h1><a href="index.php?id='.$row['id'].'">'.$row['title'].'</a></h1>';
        echo '<p>'.$row['body'].'</p>';
        }
    else:
        echo '<p>Oops, post not found!</p>';
    endif;
    if(isset($return)){
        echo $return;
        }
    }

    function add_content($p){
        $title = mysql_real_escape_string($p['title']);
        $body = mysql_real_escape_string($p['body']);

        if(!$title OR !$body):
            if(!$title):
                echo "<p>You have to fill the title.</p>";
            endif;
            if(!$body):
                echo "<p>You have to fill the body.</p>";
            endif;
            echo '<p><a href="add-content.php">Try again!</a></p>';
            else:
                $sql = "INSERT INTO content VALUES (null, '$title', '$body')";
                $res = mysql_query($sql) OR DIE (mysql_error());
                echo "Added sucessfully!";
            endif;

    }

    function manage_content()
    {
        echo '<div id="manage">';
        $sql = "SELECT * FROM content";
        $res = mysql_query($sql) or die(mysql_error());
        while($row = mysql_fetch_assoc($res)): ?>
            <div>
                <h2 class="title"><?php $row['title'] ?></h2>
                <span class="actions"><a href="#">Edit</a> | <a href="#">Delete</a></span>
        </div>  <?php
        endwhile;
        echo '</div>'; //End of Manage Div

    }

}// End of Class
?>

1 个答案:

答案 0 :(得分:1)

您忘记了echo函数中的manage_content()

<?php $row['title'] ?>

应该是:

<?php echo $row['title']; ?>

无论如何,这都是问题的一部分。