Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display first 50 characters only in php,mysql [duplicate]

Tags:

php

mysql

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?

like image 959
Mahmood Mohammed Avatar asked Dec 17 '15 08:12

Mahmood Mohammed


People also ask

How to display first 50 characters in php?

using strlen ( string $comment ) : int php $comment = 'Note: We convert a string into an array with explode function. I do not use explode function then the output will be a string as shown in below example. '; $comment = (strlen($comment) > 50)? substr($comment,0,25).

How can I get only 100 characters of a string in PHP?

$small = substr($big, 0, 100);

How do I select the first letter in mysql?

To fetch the first alphabet from the strings, use LEFT(). This method allows you to return characters from the left of the string.


1 Answers

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>";
  }
  }
like image 122
Syed mohamed aladeen Avatar answered Sep 26 '22 14:09

Syed mohamed aladeen