Solved

Limit Characters on a PHP MySQL query

Posted on 2011-02-22
7
258 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

828 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