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
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'];

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.