Ghamdan Posted October 16, 2012 Share Posted October 16, 2012 I want to retrieve the first 30 characters from mysql field text but I do not know how to do it. some of the code: echo <p> Content: ' . $row['content ']. '</p> '; How can I retrieve the first 30 characters from mysql ? Thank you. Link to comment Share on other sites More sharing options...
Antonio Conte Posted October 16, 2012 Share Posted October 16, 2012 PHP: echo <p> Content: ' . substring($row['content '], 0, 30). '</p> '; MySQL: SELECT SUBSTRING(content, 31) AS content 1 Link to comment Share on other sites More sharing options...
Ghamdan Posted October 17, 2012 Author Share Posted October 17, 2012 Thank you Antonio Conte, You are really helpful. That is great and it is working well. And thanks to the forum. Link to comment Share on other sites More sharing options...
Ghamdan Posted October 17, 2012 Author Share Posted October 17, 2012 There is a small issue with this, it returns a symbol as you can see � at the end. How can I remove � the question mark at the end? Thank you. Link to comment Share on other sites More sharing options...
rob Posted October 17, 2012 Share Posted October 17, 2012 Check you're using the same character sets, also have a look at the following if you're still having issues: http://php.net/manua...n.mb-substr.php Link to comment Share on other sites More sharing options...
Ghamdan Posted October 17, 2012 Author Share Posted October 17, 2012 I am using the charset =utf8 and the question mark returns once at the end of some posts not on all of them. The issue could be , returning the beginning letters of a word which is in set(30). Link to comment Share on other sites More sharing options...
Larry Posted October 17, 2012 Share Posted October 17, 2012 It's almost certainly a character set issue. Make sure you're using the same character set for the connection as the database table uses and as your HTML page uses. Link to comment Share on other sites More sharing options...
Ghamdan Posted October 17, 2012 Author Share Posted October 17, 2012 I am using charset utf8 for database connection, sql database, mysqli table of content and html but the question mark shows at the end of some posts and it returns one question mark only the reset of the text is fine. Some of the output: Article: المغناطيسية. الإعلان جميل جداً، ولكنه يفتقر إلى الشمولية والوضوح، فلو أن أحدهم لم يعرف عن سيرفيس وميزاته الداخلية البرمجية والصلبة من قبل، فلن يلفت انتباهه في الإعلان سوى الرقص والراقصين. وذكرت مايكروسوفت في نهاية الإعلان موعد إطلاق الحاسب رسمياً وهو السادس والعشرين م� ... This is Arabic. .............................. Thank you. Link to comment Share on other sites More sharing options...
rob Posted October 17, 2012 Share Posted October 17, 2012 Ghamdan, you don't say which method you're using to select part of the string, also did you look at the link I provided? Link to comment Share on other sites More sharing options...
Ghamdan Posted October 17, 2012 Author Share Posted October 17, 2012 I am using this one: MySQL: SUBSTRING( content, 501) AS content, PHP: <p> Article: ' . substr($row['content'], 0, 500) . ' ...' . ' </p> Thank you Link to comment Share on other sites More sharing options...
Larry Posted October 17, 2012 Share Posted October 17, 2012 You should do it in your database query. I suspect PHP is not breaking on the last character reliably. Or, if you're going to do in in PHP, then break it on a nearby space or punctuation. Link to comment Share on other sites More sharing options...
Ghamdan Posted October 18, 2012 Author Share Posted October 18, 2012 I have done some changes as follows: php:mb_substr($row['content'], 0, 300, 'UTF-8' ) mysql: SUBSTRING( content, 1,300) Thank you everyone and the great author Larry. Link to comment Share on other sites More sharing options...
Ghamdan Posted October 18, 2012 Author Share Posted October 18, 2012 It is working well now. Link to comment Share on other sites More sharing options...
Antonio Conte Posted October 18, 2012 Share Posted October 18, 2012 Glad you solved it. Sorry about that, but I offered you two different solutions. One for PHP, and one for MySQL. Use only one of them. I would also look at MySQL(i)_set_charset. Link to comment Share on other sites More sharing options...
Ghamdan Posted October 18, 2012 Author Share Posted October 18, 2012 Thank you Link to comment Share on other sites More sharing options...
Larry Posted October 18, 2012 Share Posted October 18, 2012 Glad it's working and thanks for letting us know. Thanks, too, for the nice words. Link to comment Share on other sites More sharing options...
Recommended Posts