1

我有这行代码:

$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 个字符,它会查询除最后一个单词之外的所有字符。这是我用来回显查询的代码(它在带有 db 的 while 循环中)

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

1 回答 1

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
于 2013-09-09T16:02:27.097 回答