Get first NR letters from mysql row

Discuss coding issues, and scripts related to PHP and MySQL.
User avatar
JanMolendijk
Posts:282
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>
<?php
// connect to the "tests" database
$conn = new mysqli('127.0.0.1', '', '', '');

// 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="http://145.53.93.209/News/index.php?itemid='. $row['citem']. '#c" title="Show Comment" class="postlink" target="home">'. $row['ctime']. '</a> '. $row['cbody']. '  '. $row['cuser']; 
  }
}
else {
  echo '0 results';
}

$conn->close();
?>

Admin Posts:805
Hello,
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";

JanMolendijk Posts:282
Thank you so Mutch for Suporting dear Admin
You really deserve a donation