Truncation of fields from database

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
royalblue
Forum Newbie
Posts: 13
Joined: Sun May 16, 2004 8:26 am

Truncation of fields from database

Post by royalblue »

I am using Dreamweaver mx 2004, MYSQL & Php

I have a results page on my website that displays text from my database etc. I want to truncate one of the text fields so that it only displays the first 50 characters. I've heard that it can be done in MYSQL as follows:

SELECT LEFT(long_field, 50) AS preview FROM your_table

or alternatively in PHP as follows:

$preview = substr($whole_field, 0, 50);


My question is how to incorporate these functions into my sql queries/PHP Code:

SELECT *
FROM MyTable
WHERE MyField LIKE '%colname%'

<?php echo $row_rsRecordset['Myfield']; ?>


The preview function in sql does not seem to work well with the dreamweaver mx repeat region funtionality and just shows the first record on the database. I'd prefer to do this in PHP if possible. I'm quite new to coding so please be gentle with replies!
lostboy
Forum Contributor
Posts: 329
Joined: Mon Dec 30, 2002 8:12 pm
Location: toronto,canada

Post by lostboy »

RTFM

Code: Select all

LEFT(str,len) 
Returns the leftmost len characters from the string str. 
mysql> SELECT LEFT('foobarbar', 5);
        -> 'fooba'
SELECT LEFT(colname,50)
FROM MyTable
WHERE MyField LIKE '%colname%'
royalblue
Forum Newbie
Posts: 13
Joined: Sun May 16, 2004 8:26 am

Post by royalblue »

Thanks for reply but this funtion doesn't seem to work witha 'repeat region' server behaviour in dreamweaver - it just keeps returning the same field over and over again. Is there any way of doing this in php?
royalblue
Forum Newbie
Posts: 13
Joined: Sun May 16, 2004 8:26 am

Post by royalblue »

Have now got this working - Many thanks for your help !
Post Reply