?
Solved

Limit Characters on a PHP MySQL query

Posted on 2011-02-22
7
Medium Priority
?
262 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

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 2000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
In this article, we’ll look at how to deploy ProxySQL.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

800 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