仅在php,mysql中显示前50个字符

时间:2015-12-17 08:23:38

标签: php mysql

我在数据库表,标题和说明中有两个字段。我在循环中在php中显示数据。

我的代码:

$sql = "select * from sightseeing";
    $i = 1;
    $res = mysql_query($sql, $conn); 
    if( mysql_num_rows($res) > 0) {
        while($row = mysql_fetch_array($res))
       {

        echo "<tr>
          <td>".$i++."</td>
          <td>".$row["title"]."</td>
          <td>".$row["description"]."</td>
        </tr>";
      }
   }

我想只显示描述字段中的前50个字符。怎么做?

2 个答案:

答案 0 :(得分:2)

试试这个

 $sql = "select * from sightseeing";
$i = 1;
$res = mysql_query($sql, $conn); 
if( mysql_num_rows($res) > 0) {
    while($row = mysql_fetch_array($res))
{

echo "<tr>
  <td>".$i++."</td>
  <td>".$row["title"]."</td>
  <td>".substr($row['description'], 0, 50)."</td>
 </tr>";
  }
  }

答案 1 :(得分:1)

使用MySQL LEFT

select *,LEFT(description , 50) description from sightseeing
相关问题