为什么我的查询不能用于检索子字符串?

unf*_*ven -2 html php mysql

以下是我显示用户评论的查询.效果很好:

enter 
$query='SELECT
customers.cust_id,
customers.f_name,
customers.l_name,
user_comments.comment_txt,
clubs.name_club
from
customers
    inner join
user_comments
    on customers.cust_id = user_comments.cust_id
    inner join
clubs
    on user_comments.cust_id = clubs.cust_id
    ORDER BY RAND() LIMIT 1,1
    ';

$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
$nameclub =$row['name_club'];
$comment =$row['comment_txt'];
echo '<div><img src="images/arrow.gif">&nbsp;<a   href="news.php?id='.$row['cust_id'].'">&nbsp;Name : '.$nameclub.'</a></div>';
echo '<div>'.$comment.'</div><br>';
echo '<div>'.$row['f_name'].' '.$row['l_name'].'</div>';
}
echo '<p style="text-align: left"><a href="newsarchive.php">&nbsp;Show all comments</ ></a></p> '; ?>
Run Code Online (Sandbox Code Playgroud)

我现在想要显示用户评论的大约100个字符.所以我相应地更改了我的查询,但它不起作用:

enter $query='SELECT
customers.cust_id,
customers.f_name,
customers.l_name,
user_comments.comment_txt.substr(comment_txt,1,100) as comment_txt,
clubs.name_club
from
customers
    inner join
user_comments
    on customers.cust_id = user_comments.cust_id
    inner join
clubs
    on user_comments.cust_id = clubs.cust_id
    ORDER BY RAND() LIMIT 1,1
    ';

$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
$nameclub =$row['name_club'];
$comment =$row['comment_txt'];
 echo '<div><img src="images/arrow.gif">&nbsp;<a href="news.php?id='.$row['cust_id'].'">&nbsp;Name : '.$nameclub.'</a></div>';
 echo '<div>'.$comment.'</div><br>';
 echo '<div>'.$row['f_name'].' '.$row['l_name'].'</div>';
 }
 echo '<p style="text-align: left"><a href="newsarchive.php">&nbsp;Show all comments</ ></a></p> ';   ?>
Run Code Online (Sandbox Code Playgroud)

如何修复我的查询以获得评论的100个字符?

eli*_*ide 5

您的使用SUBSTR()不正确.替换这个:

user_comments.comment_txt.substr(comment_txt,1,100) as comment_txt,
Run Code Online (Sandbox Code Playgroud)

有了这个:

SUBSTR(user_comments.comment_txt,1,100) as comment_txt,
Run Code Online (Sandbox Code Playgroud)

每条评论编辑:要添加...文本是否继续超过100个字符,请使用

CONCAT(SUBSTR(user_comments.comment_txt,1,100), IF(LEN(user_comments.comment_txt) > 100, '...', '')) as comment_txt,
Run Code Online (Sandbox Code Playgroud)