【问题标题】:Display first 50 characters only in php,mysql [duplicate]仅在php,mysql中显示前50个字符[重复]
【发布时间】:2015-12-17 08:23:38
【问题描述】:

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

我的代码:

$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 个字符。该怎么做?

【问题讨论】:

标签: php mysql


【解决方案1】:

试试这个

 $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>";
  }
  }

【讨论】:

  • 不客气,请您在我的回答上打勾
【解决方案2】:

使用 MySQL LEFT

select *,LEFT(description , 50) description from sightseeing

【讨论】:

    猜你喜欢
    • 2015-11-17
    • 1970-01-01
    • 2019-10-13
    • 1970-01-01
    • 2015-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-13
    相关资源
    最近更新 更多