如何只从mysql中保存的名为body的行回显几行?

Basically I want to echo only summary of my blog post on a certain page by making a function() that must limit the number of counts of words as specified there.

Save the summary and body of your blog posts in different columns.

function sumarize($your_string){
   $count++;
   $maximum = 10;
   foreach(explode("
", $your_string) as $line){
       $count++;
       echo $line."
";
       if ($count == $maximum) break;        
   }
}

this one takes into account numbers of character whilst ending at the last word without cutting out a character

use

select .... SUBSTR(body,1,300) .....

later you can use this function in php to cut the string at the last space or period so you wont get a half cut word in the end. The second parameter is the number of characters you want.

function shorten_string($string, $characters)
{
    $shortened_string = "";
    $smaller_string = substr($string, 0, $characters);
    $pos_of_last_space = strrpos($smaller_string, " ");
    $pos_of_last_break = strrpos($smaller_string, " ");

    if (strlen($string) <= $characters) {
        $shortened_string = $string;
    } elseif (!$pos_of_last_space && !$pos_of_last_break) {
        $shortened_string = $smaller_string;
    } else {
        $break_at = 0;
        if ($pos_of_last_space > $pos_of_last_break) {
            $break_at = $pos_of_last_space;
        } else {
            $break_at = $pos_of_last_break;
        }

        $shortened_string = substr($smaller_string, 0, $break_at);
    }
}

NOTE: takes care of spaces put in html with 'nbsp'

Lets say your table (named main) looks like that.

id | blogpost

1 sample1

2 sample2 ...

At first you need to connect to db

$db=NEW MYSQLI('localhost', 'username', 'pass', 'dbname') or die ($db->error);

Then write following piece of code

function sumarize($your_string){
   $count++;
   $maximum = 10;
   foreach(explode("
", $your_string) as $line){
       $count++;
       echo $line."
";
       if ($count == $maximum) break;        
   }
}

$result=$db->query("SELECT `id`, `blogpost` FROM `main`");
while($row->fetch_object()){
echo sumarize($row->blogpost);
}

This is how to get work genesis φ's solution