6

I have two fields in database table, title and description. I am displaying tha data in php while loop.

My Code :

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

I want to show only first 50 characters from description field. How to do that?

Mahmood Mohammed
  • 187
  • 2
  • 4
  • 14

2 Answers2

4

try this

 $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>";
  }
  }
Syed mohamed aladeen
  • 6,507
  • 4
  • 32
  • 59
1

use MySQL LEFT

select *,LEFT(description , 50) description from sightseeing
Abhishek Sharma
  • 6,689
  • 1
  • 14
  • 20