Forum Moderators: coopster

Message Too Old, No Replies

Is there a SUBSTRING Gremlin?

works without SUBSTRING added

         

henry0

6:41 pm on Dec 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Inspired by a recent post I would like applying a SUBSTRING query to a segment of my admin in order to display only a text sample for ex: the first 100 or 200 characters
I try using the following:
<<<
$result = mysql_query("select topic_id, month, day, year, excerpt, title, user_id, unique_page, SUBSTRING ('main _content',1,100)
from $database_table where topic_id = $last_topic_id",$db)
>>>
I tried with or without quotes and even LEFT and reviewed the manual.
But nothing's working
Do I have a query syntax error?

PS
If I rem the SUBSTRING query it all works fine

Thanks

Henry

baze22

7:00 pm on Dec 30, 2004 (gmt 0)

10+ Year Member



First off, I took your query changed it a little to work with a table I already had. Noticed that the single quotes needed to com off of the 'main_content' of your function call. I'd give the result of the function a column name for clarity so it would end up like:

$result = mysql_query("select topic_id, month, day, year, excerpt, title, user_id, unique_page, SUBSTRING (main _content,1,100) as substrvar
from $database_table where topic_id = $last_topic_id",$db)

baze

davelms

7:02 pm on Dec 30, 2004 (gmt 0)

10+ Year Member



Don't know, really. I was looking at the similar discussion yesterday, I had to admit I'd never used this function before (so used PHP myself to achieve the same) but I just tried it on my own site and it seems to work for me:-


SELECT substring( articletext, 1, 6 ) FROM article

Sorry if that's not much use, I just looked at the manual and picked that syntax from one of the user comments attached to the appropriate manual page.

[edit] beaten to it :) must type faster... [/edit]

ergophobe

7:32 pm on Dec 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



henry0,

if you put the column name in quotes in the SUBSTRING call, it treats that as the string, rather than the data from that column.

If the data in 'my_long_column' is "some comments by henry0"

SUBSTRING('my_long_column', 1, 6)
> 'my_lon'

SUBSTRING(my_long_column, 1, 6)
> 'some c'

henry0

9:45 pm on Dec 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks all, got it
Sorry I did not aknowledged your posts earlier
possibly I forgot clicking on notification :)

Henry