kvnirvana Posted November 13, 2010 Share Posted November 13, 2010 Can't seem to get this code to work. What I want to do is to get the first 5 letters from comment. I get this message Undefined index: comment I know it has something to do with this part LEFT('comment', 5) because if I remove it it shows the result. <?php echo '<p ><strong>Seneste kommentarer</strong></p>'; mysql_connect("", "", "") or die("Could not connect: " . mysql_error()); mysql_select_db(""); $result = mysql_query ("SELECT * LEFT('comment', 5) from comments where user_id='51'" ) or die(mysql_error()); echo "<table border='1'>"; echo "<tr> <th>Comments</th> </tr>"; // keeps getting the next row until there are no more to get while($row = mysql_fetch_array( $result )) { // Print out the contents of each row into a table echo "<tr><td>"; echo $row['comment']; echo "</td><td>"; } echo "</table>"; Quote Link to comment https://forums.phpfreaks.com/topic/218577-limit-words/ Share on other sites More sharing options...
Pikachu2000 Posted November 13, 2010 Share Posted November 13, 2010 Alias the comment field in the query, then refer to the alias in the array index. SELECT * LEFT('comment', 5) AS truncated FROM . . . echo $row['truncated']; Quote Link to comment https://forums.phpfreaks.com/topic/218577-limit-words/#findComment-1133802 Share on other sites More sharing options...
kvnirvana Posted November 13, 2010 Author Share Posted November 13, 2010 Ok I've got this $result = mysql_query ("SELECT * LEFT('comment', 5) AS truncated from comments where user_id='51'" ) or die(mysql_error()); echo "<table border='1'>"; echo "<tr> <th>Comments</th> </tr>"; // keeps getting the next row until there are no more to get while($row = mysql_fetch_array( $result )) { // Print out the contents of each row into a table echo "<tr><td>"; echo $row['truncated']; echo "</td><td>"; } echo "</table>"; But I get this error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LEFT('comment', 5) AS truncated from comments where user_id='51'' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/218577-limit-words/#findComment-1133808 Share on other sites More sharing options...
Pikachu2000 Posted November 13, 2010 Share Posted November 13, 2010 Oops, needs a comma after the wildcard *. Also change the single quotes around 'comment' to `backticks` Quote Link to comment https://forums.phpfreaks.com/topic/218577-limit-words/#findComment-1133810 Share on other sites More sharing options...
kvnirvana Posted November 13, 2010 Author Share Posted November 13, 2010 Thanks :=) Quote Link to comment https://forums.phpfreaks.com/topic/218577-limit-words/#findComment-1133831 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.