Showing Part Of The Content For Description By Mysql

Post here if you need help with SQL.
Post Reply
Alg0r1thm
Posts: 64
Joined: Mon Sep 12, 2011 7:57 pm

Showing Part Of The Content For Description By Mysql

Post by Alg0r1thm »

Hi brothers ,
I have a file archive.php that contains the last posts and it shows the title of the subjects but I wanna also show the part of the subject description I mean part of the text in archive.php , Assume that is archive.php :

Betterphp
Betterphp is a free php tutorial provider ...
________________________
Youtube
Hi, in this post I introduce you with youtube ...

pages : 1 2


I want to show short description that is fetched from the database with what mysql function can I do it ?

I hope you understand ,

Thanks in advance
JelvinJS7
Posts: 341
Joined: Thu May 12, 2011 8:40 pm

Re: Showing Part Of The Content For Description By Mysql

Post by JelvinJS7 »

In the query, use the JOIN function. It works the same as the php substr function; first parameter is the field, second is the amount of characters you want.
Alg0r1thm
Posts: 64
Joined: Mon Sep 12, 2011 7:57 pm

Re: Showing Part Of The Content For Description By Mysql

Post by Alg0r1thm »

Can you show me an example ?

Thanks
JelvinJS7
Posts: 341
Joined: Thu May 12, 2011 8:40 pm

Re: Showing Part Of The Content For Description By Mysql

Post by JelvinJS7 »

I meant to say left… not join. :oops:
SELECT `title`, `date`, LEFT(`description`, 30) FROM `posts` WHERE `post_id` = {$id}

User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Showing Part Of The Content For Description By Mysql

Post by jacek »

You can also do
SELECT `title`, `date`, SUBSTRING_INDEX(`description`, ' ', 30) FROM `posts` WHERE `post_id` = {$id}
which would give you the first 30 whole words. It just looks a little nicer I think :)
Image
Post Reply