将数据库文本值输出到div中

I am trying to output values saved in my database into a div field, however the data does not seem to come out, could anyone explain to me why?

<div>
<?php
        $host="localhost"; // Host name 
        $username="root"; // Mysql username 
        $password="root"; // Mysql password 
        $db_name="Database"; // Database name 

        mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
        mysql_select_db("$db_name")or die("cannot select DB");

        $result=mysql_query("SELECT from `posts`");
        $num=mysql_numrows($result);
        $i=0;
        while ($i < $num) {
            echo $result[$i];
            $i++;
        }
        ?>
</div>

Above is the code I have inside the div tag. So my question why does my echo not work?

This is not the way to do that, you may use like this:

//$result=mysql_query("SELECT from `posts`"); <- this query is wrong. 
if ($result = mysql_query("SELECT * FROM posts"))
{
    $num=mysql_num_rows($result);

   while ($row = mysql_fetch_assoc($result)) {
       echo $row['key']; // <- you must set the field name of your query here
   } 
}
else // This if will show you when any query error is thrown.
{
    echo mysql_error();
}

Your original query is wrong. You may get an error because SELECT FROM is not valid. You have to specify a list of fields, a function or * to get all fields of the table.

The you may use mysql_fetch_assoc(), mysql_fetch_row() or mysql_fetch_array() in order to fetch the received data from the query.

Plus, I have added an if in your query command in order to get any mysql error thrown in your query execution. Using that you'll never be confuse about what happened here.

Hope it helps.

UPDATE: As @SpiderLinked said(Thank you dude), mysql_numrows() does not exists. You have to use mysql_num_rows() to get query result count.

"SELECT from `posts`"

is not a valid query.

This would be:

"SELECT * FROM `posts`"

Also the following won't pull any results from your database:

while ($i < $num) {
    echo $result[$i];
    $i++;
}

You would need to loop around the results as you fetch them:

while ($row = mysql_fetch_assoc($result)) {
    echo $row['field'];
}

There are several errors with your code...first....you do not give the result of the query to any variable...second...your query is wrong...third... there is no such function as mysql_numrows()....here is an example of what it should look like:

<div>
  <?php
    $host="localhost"; // Host name 
    $username="root"; // Mysql username 
    $password="root"; // Mysql password 
    $db_name="Database"; // Database name 

    mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB");

    $result=mysql_query("SELECT * FROM posts") or die (mysql_error()); //added die for debugging purposes
    while ($num=mysql_fetch_array($result)) {
        echo $num['COLUMN NAME HERE!'];
    }
    ?>

Also...please stop using mysql_ while it is deprecated...

try this....

<div>
<?php
    $host="localhost"; // Host name 
    $username="root"; // Mysql username 
    $password="root"; // Mysql password 
    $db_name="Database"; // Database name 

    $result=mysql_query("SELECT * FROM posts");
    $num=mysql_numrows($result);
    if($num>0) {
        echo $num." rows returned";
        while ($row = mysql_fetch_assoc($result)) {
            echo $row['key'];
        }
    } else {
        echo "No Rows Returned";
    }

    ?>
</div>