suttercain Posted April 7, 2007 Share Posted April 7, 2007 Hi everyone, I am trying to limit the amount of text exhoed from a MySQL database column. From reading around it appears the substr() should be able to do this. I have my code set up as such: <?php $sql = mysql_query ("SELECT title, story_id, story_date, body FROM news ORDER BY story_id DESC LIMIT 0, 3"); $body = $row['body']; $short = substr('$body', 0, 4); while ($row = mysql_fetch_assoc($sql)){ echo "<b>" . $row['story_date'] . "</b><br>"; echo "<a href='view_news.php?id={$row['story_id']}'>" . ucwords(strtolower($row['title'])) . "</a><br>"; echo $short . "<br>"; } ?> I assign the query result from the column to $body. I then use the substr function and assign it a variable of short. But when I echo $short it displays: $bod which it is displaying only the 4 characters but it's not echoing the MySQL query result, instead only the first four of the variable. Can this be done with substr? If not what am I doing wrong? If not any suggestions. Thanks. Link to comment https://forums.phpfreaks.com/topic/46020-solved-substr-with-a-mysql-result/ Share on other sites More sharing options...
suttercain Posted April 7, 2007 Author Share Posted April 7, 2007 Got it. <?php $sql = mysql_query ("SELECT title, story_id, story_date, body FROM news ORDER BY story_id DESC LIMIT 0, 3"); while ($row = mysql_fetch_assoc($sql)){ $body = $row['body']; $short = substr("$body", 0, 4); echo "<b>" . $row['story_date'] . "</b><br>"; echo "<a href='view_news.php?id={$row['story_id']}'>" . ucwords(strtolower($row['title'])) . "</a><br>"; echo $short . "<br>"; } ?> Link to comment https://forums.phpfreaks.com/topic/46020-solved-substr-with-a-mysql-result/#findComment-223604 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.