Solved

Limit Characters on a PHP MySQL query

Posted on 2011-02-22
7
255 Views
Last Modified: 2012-05-11
I need to limit the amount of characters show on a query. The amount of characters I need to show is 100 from the 'copy' column

Here's my query code:

mysql_select_db($database_Hollybush, $Hollybush);
$query_news = "SELECT id, `date`, title, copy, FROM news ORDER BY id DESC";
$news = mysql_query($query_news, $Hollybush) or die(mysql_error());
$row_news = mysql_fetch_assoc($news);
$totalRows_news = mysql_num_rows($news);

I have tried this but nothing shows?

mysql_select_db($database_Hollybush, $Hollybush);
$query_news = "SELECT id, `date`, title, SUBSTR(copy,50,50) FROM news ORDER BY id DESC";
$news = mysql_query($query_news, $Hollybush) or die(mysql_error());
$row_news = mysql_fetch_assoc($news);
$totalRows_news = mysql_num_rows($news);

Anyone know how to do this?
0
Comment
Question by:BrighteyesDesign
  • 4
  • 3
7 Comments
 
LVL 9

Expert Comment

by:AriMc
ID: 34954583
Try this:

"SELECT id, `date`, title, LEFT(copy,100) FROM news ORDER BY id DESC";
0
 

Author Comment

by:BrighteyesDesign
ID: 34954644
Thanks for that.

It doesn't show any result i'm afraid.

The text shows with the first of my two codes above by the way so everything else is ok.
0
 
LVL 9

Expert Comment

by:AriMc
ID: 34954680
That is strange since the first statement contains a syntax error (improper comma between "copy" and "from):

   "SELECT id, `date`, title, copy, FROM news ORDER BY id DESC";

What is the type of that copy column, ie. how did you create it?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:BrighteyesDesign
ID: 34954725
HI,

Yes, you're right, I actually deleted that comma after posting.

I created it in MySQL as text (see screenshots) MySQLScreen-shot-2011-02-22-at-19.14..png
0
 
LVL 9

Expert Comment

by:AriMc
ID: 34954785
I tried to simulate it on my MySQL instance:

   drop table test;
   create table test (a int, copy text);
   insert into test values (1, 'abcde fghij klmno');
   select a,copy from test;
   select a, left(copy,5) from test;

The first select returns:

  1, abcde fghij klmno

and the second:

  1, abcde

just like expected.

By "no results" do you mean the column selected as left(copy,100) is empty but other colums show right or that there are no rows returned at all?


0
 
LVL 9

Accepted Solution

by:
AriMc earned 500 total points
ID: 34954800
You could also try:

"SELECT id, `date`, title, LEFT(copy,100) as copy FROM news ORDER BY id DESC";
0
 

Author Closing Comment

by:BrighteyesDesign
ID: 34955951
Perfect, thanks a lot for that!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now