从数据库中仅查询400个字符将不起作用

时间:2013-09-09 16:00:52

标签: php mysql

我有这行代码:

$query=$db->prepare("SELECT post_id, title, LEFT(body, 400), category FROM posts INNER JOIN categories ON categories.category_id=posts.category_id ORDER BY post_id DESC LIMIT $start, $per_page");

LEFT(body,400)仅查询400个字符,它将查询除最后一个字之外的所有字符。 这是我用来回显查询的代码(它在while循环w / db中)

<p><?php
     $body_sub = substr($body, 0). "...<a href='post.php?id=$post_id'> Continue Reading →</a>";
    echo nl2br($body_sub);
?></p>

1 个答案:

答案 0 :(得分:3)

在函数中使用列body在技术上不存在。您需要为列添加别名:

... LEFT(body, 400) AS body

然后致电body

我还建议在系统术语中使用反引号,例如列:

SELECT `post_id`, 
       `title`, 
       LEFT(`body`, 400) AS `body`, 
       `category`
FROM   `posts`
       INNER JOIN `categories` 
               ON `categories`.`category_id` = `posts`.`category_id` 
ORDER  BY `post_id` DESC 
LIMIT  $start, $per_page