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" Quote Link to comment 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']; Quote Link to comment 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]; } Quote Link to comment Share on other sites More sharing options...
woodplease Posted February 8, 2012 Author Share Posted February 8, 2012 ah rite,ye, thanks both Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.