Jump to content

displaying first 50 characters of field


woodplease

Recommended Posts

Hi, i'm trying to retrieve and display just the first 50 characters of a field in a table, but when i try to display the result, i keep getting an undefined index error.

$text =mysql_query("SELECT LEFT(entry_text,50) FROM entry WHERE entry_id='22'") or die(mysql_error());
     while($row2 = mysql_fetch_array($text)){
     echo "<p>".$row2['entry_text'];
     }

When i leave out the " LEFT(entry_text,50)", so that it retrieves all the characters, it works fine. Any ideas?

the exact error i get is

"Notice: Undefined index: entry_text in C:\wamp\www\new\index.php on line 52"

Link to comment
https://forums.phpfreaks.com/topic/256675-displaying-first-50-characters-of-field/
Share on other sites

Because you'd need to alias the field in the query string with AS, then use the alias when working with the value. If you print_r() the array, you'll see it isn't what you think it is.

 

SELECT LEFT(entry_text,50) AS left50 FROM entry WHERE entry_id='22'
echo "<p>".$row2['left50'];

you don't reference the LEFT() functions output by the actual field name, by by the entire function..

 

echo "<p>".$row2['LEFT(entry_text,50)'];

 

but I would just alias it.

 

$text =mysql_query("SELECT LEFT(entry_text,50) as fifty_chars FROM entry WHERE entry_id=22") or die(mysql_error());
while($row2 = mysql_fetch_array($text))
{
echo "<p>".$row2['fifty_chars];
}

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.