Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The problem you have with your code is that you have some badly named variables. You assign the result resource returned by <code>mysql_query()</code> to <code>$result</code>, you then try and fetch a row from a variable called <code>$message</code> (which you don't show the assignment of) and you then switch to treating <code>$message</code> as if it were a string, rather than manipulating the value in <code>$row</code>.</p> <p>Regardless of that, if you just want to fetch the string from the DB and apply an ellipsis, and you don't need the full text, I would get MySQL to do the work for me:</p> <pre><code>SELECT IF(CHAR_LENGTH(`comment`) &gt; 10, CONCAT(SUBSTRING(`comment`, 1, 10), '...'), `comment`) AS `comment` FROM `Assembly` WHERE `title` = 'Checkered' ORDER BY `date` DESC LIMIT 1 </code></pre> <p>In PHP:</p> <pre><code>$query = " SELECT IF(CHAR_LENGTH(`comment`) &gt; 10, CONCAT(SUBSTRING(`comment`, 1, 10), '...'), `comment`) AS `comment` FROM `Assembly` WHERE `title` = 'Checkered' ORDER BY `date` DESC LIMIT 1 "; $result = mysql_query($query); while ($row = mysql_fetch_assoc($result)) { echo "Last Post:&lt;br&gt;"; echo htmlspecialchars($row['comment']); } </code></pre> <p><strong>EDIT</strong></p> <p>Following @Jared's comments about the potential for broken strings displaying partial HTML tags, here is the way to do it that will avoid this:</p> <pre><code>$query = " SELECT `comment` FROM `Assembly` WHERE `title` = 'Checkered' ORDER BY `date` DESC LIMIT 1 "; $result = mysql_query($query); // Loop is pointless because of the LIMIT 1 $row = mysql_fetch_assoc($result); $maxlen = 10; // Max length before trim $comment = strip_tags($row['comment']); if (strlen($comment) &gt; $maxlen) { $comment = substr($comment, 0, $maxlen).'...'; } $comment = htmlspecialchars($comment); echo "Last Post:&lt;br&gt;"; echo $comment; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload