Get first NR letters from mysql row

Discuss coding issues, and scripts related to PHP and MySQL.
User avatar
Posts: 169
Location: Holland Rotterdam

Get first NR letters from mysql row

Dear Admin i hope you can help me out...

I have a list off latest comments but some text are to long & i would like to show only the first 10 caracts from '. $row['cbody']. ' , so i don`t see the whole comment but just the first 10 letters into '. $row['cbody']. '
How can this be created ???

Code: Select all

<b>Latest Comments</b>
// connect to the "tests" database
$conn = new mysqli('', '', '', '');

// check connection
if (mysqli_connect_errno()) {
  exit('Connect failed: '. mysqli_connect_error());

// SELECT sql query
$sql = "SELECT `ctime`, `citem`, `cuser`, `cbody` FROM `nucleus_comment` ORDER BY `nucleus_comment`.`ctime` DESC"; 

// perform the query and store the result
$result = $conn->query($sql);

// if the $result contains at least one row
if ($result->num_rows > 0) {
  // output data of each row from $result
  while($row = $result->fetch_assoc()) {
    echo ' <br /><br /> <a href="'. $row['citem']. '#c" title="Show Comment" class="postlink" target="home">'. $row['ctime']. '</a> '. $row['cbody']. '  '. $row['cuser']; 
else {
  echo '0 results';


Try with the SUBSTRING(column_name, start, length) function.

Code: Select all

$sql = "SELECT `ctime`, `citem`, `cuser`, SUBSTRING(`cbody`, 1, 10) AS cbody FROM `nucleus_comment` ORDER BY `nucleus_comment`.`ctime` DESC";

Thank you so Mutch for Suporting dear Admin
You really deserve a donation

Similar Topics