我想知道如何缩短从 mysql 数据库生成的文本。我尝试按照在互联网上找到的一些教程来做到这一点,但失败了。
这是我的代码,
<?php
$dbh=mysql_connect("localhost","root","root") or die ('Cannot connedt to the Database' .mysql_errno());
mysql_select_db("myinfo_db");
$res_query = mysql_query("SELECT A.cat_id as cat_id, count(A.cat_id) as cnt, B.category as category FROM listings A, category B WHERE A.cat_id=B.cat_id GROUP BY A.cat_id");
while ($category = mysql_fetch_assoc($res_query) )
{
echo '<a href="page.php?cat_id='.$category['cat_id'].'">'.$category['category'].' ('.$category['cnt'].')</a><br />';
}
?>
最佳答案
如果我正确理解你的问题,你可以使用 MySQL 子字符串函数:
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html
SELECT CONCAT(SUBSTRING(sometext,1,10), '...')
FROM sometable;
其中 1 是起始位置,10 是字符数。
这样就可以直接做,不需要php来做。 在你的情况下类似:
SELECT A.cat_id as cat_id, count(A.cat_id) as cnt, CONCAT(SUBSTRING(B.category,1,10), '...') as category FROM listings A, category B WHERE A.cat_id=B.cat_id GROUP BY A.cat_id
关于php - 无法缩短 php 和 mysql 中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25662385/