Jump to content

[SOLVED] substr() with a MySQL result?


suttercain

Recommended Posts

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

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>";
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.