woodplease Posted February 8, 2012 Share Posted February 8, 2012 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 More sharing options...
Pikachu2000 Posted February 8, 2012 Share Posted February 8, 2012 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']; Link to comment https://forums.phpfreaks.com/topic/256675-displaying-first-50-characters-of-field/#findComment-1315833 Share on other sites More sharing options...
AyKay47 Posted February 8, 2012 Share Posted February 8, 2012 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]; } Link to comment https://forums.phpfreaks.com/topic/256675-displaying-first-50-characters-of-field/#findComment-1315835 Share on other sites More sharing options...
woodplease Posted February 8, 2012 Author Share Posted February 8, 2012 ah rite,ye, thanks both Link to comment https://forums.phpfreaks.com/topic/256675-displaying-first-50-characters-of-field/#findComment-1315836 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.