473,385 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

how to show limited numbers of text from a mysql column ?

khalidbaloch
is this possible to show limted no number of text from a mysql record .
for example i run this query
SELECT DISTINCT short_description FROM news_contents ORDER BY postdate DESC LIMIT 1

and if the record's length is greater than 50 i dont want to print any more text after 50 for example

12345678910111213141516171819202122232425262728293 03132333435363738394041424344454647484950......... ...........

thanx for any help in advance ...........
Oct 19 '06 #1
9 5020
miller
1,089 Expert 1GB
Danger, Will Robinson!

First off, what you ultimately need is the SUBSTR function, which you can read about at: http://dev.mysql.com/doc/refman/5.1/en/string-functions.html

There is a flaw in your SQL statement though, and it revolves around the dual use of DISTINCT with an ORDER BY. Whenever the terms that you sort by are not included within the DISTINCT clause, the results cannot be predicted. Any values for "description" that are duplicated could come in the order specified by any of the records in that group. There is no predicting the results. You can read more about it at:
http://dev.mysql.com/doc/refman/5.1/en/distinct-optimization.html
http://dev.mysql.com/doc/refman/5.1/en/group-by-hidden-fields.html

I would therefore suggest that you simple remove the DISTINCT call altogether. This is especially true since you currently have a LIMIT of 1. Guess what, if you only pull 1 record, you can assume that it's "distinct". Amazing logic, yes?

I'll leave you with two SQL statements though. One of them will remove the distinct requirement and only pull 1 record. The second will pull 10 records and require that they be distinct. But once again, the second query will have unpredictable results for any "non-distinct" records.

SELECT SUBSTR(short_description, 0, 50) FROM news_contents ORDER BY postdate DESC LIMIT 1;

SELECT SUBSTR(short_description, 0, 50) FROM news_contents GROUP BY short_description ORDER BY postdate DESC LIMIT 10;
Oct 19 '06 #2
i read the manual that you refere and took some tutorials
when i ran the following query

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include ("connection.ini.php");
  3. $result = mysql_query("SELECT SUBSTR(short_description, 0, 50) FROM news_contents GROUP BY short_description ORDER BY postdate DESC LIMIT 10") 
  4. or die(mysql_error());  
  5. while($row = mysql_fetch_array( $result )) {
  6.     echo $row['description'];
  7. }
  8. ?>
i did not see any records on the page ,i also try by changing mysql_query
to

SELECT SUBSTR(short_description, 0, 50) FROM news_contents ORDER BY postdate DESC LIMIT 1;

and

SELECT SUBSTR(short_description,0,50) FROM news_contents LIMIT 1

but no record where printed
please make an example query with comlete code .. thanx
Oct 20 '06 #3
sorry i missed short_ in echo "$row['description'] it was actuly echo "$row['short_description']"; so that is not the cause of disappearance of records.
i read the manual that you refere and took some
tutorials
when i ran the following query

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include ("connection.ini.php");
  3. $result = mysql_query("SELECT SUBSTR(short_description, 0, 50) FROM news_contents GROUP BY short_description ORDER BY postdate DESC LIMIT 10") 
  4. or die(mysql_error());  
  5. while($row = mysql_fetch_array( $result )) {
  6.     echo $row['_description'];
  7. }
  8. ?>
i did not see any records on the page ,i also try by changing mysql_query
to

SELECT SUBSTR(short_description, 0, 50) FROM news_contents ORDER BY postdate DESC LIMIT 1;

and

SELECT SUBSTR(short_description,0,50) FROM news_contents LIMIT 1

but no record where printed
please make an examaple query with complete code .. thanx
Oct 20 '06 #4
ronverdonk
4,258 Expert 4TB
In SUBSTR the start position starts at 1 not 0, so your query should be e.g.
Expand|Select|Wrap|Line Numbers
  1. SELECT SUBSTR(short_description,1,50) FROM news_contents LIMIT 1
  2.  
Ronald :cool:
Oct 21 '06 #5
problem is still there .i ran this query
SELECT SUBSTR(short_description,1,5) FROM news_contents LIMIT 1
but it retrive no result and afterword a ran the same query from phpmyadmin and i get this error
#1305 - FUNCTION mydbname.SUBSTR does not exist
and then i change the spelling of SUBSTR(short_description,1,5)
to SUBSTRING(short_description,1,5) ,and it worked in phpmyadmin
but i but want to run the same query in my php page for this purpose i have the following code .
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include ("config.php");
  3. $result = mysql_query("SELECT SUBSTRING(short_description,1,3) FROM news_contenys LIMIT 1") 
  4. or die(mysql_error());  
  5. while($row = mysql_fetch_array( $result )) {
  6.     // Print out the contents of each row into a table 
  7.     echo $row['short_description'];
  8. }
  9. ?>
yes i agree that my thread is no more related to mysql forum , but i dont consider it good to post tha same thread in an other forum
.so when i put these codes is my php page no records retrived from the database please take a look at my php code and tell what i am missing or doing wrong in this code
i am already very thankfull to ronverdonk , he has soleved my first thread http://www.thescripts.com/forum/thread545705.html ,in php forum . and i am sure that ronverdonk will solve my this thread as well
Thanks ronverdonk please do me this fever.
Oct 22 '06 #6
ronverdonk
4,258 Expert 4TB
In your post you have your table name misspelled. Maybe that is the reason. Your statement says 'news_contenys' and it should be 'news_contents'.
But maybe it is just a typo. As far as I can see there is no other error in your code, unless the target rows disappeared mysteriously.

Ronald :cool:
Oct 22 '06 #7
yes this misspelled was made only in posting , but my problem has been solved thanks again for you personel attention, this is how the SUBSTRING function works on my local server please note the code ..
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include ("config.php");
  3. $result = mysql_query("SELECT SUBSTR(short_description,1,50) FROM news_contents") 
  4. or die(mysql_error());  
  5. while($row = mysql_fetch_array( $result )) {
  6.     echo $row['SUBSTR(short_description,1,50)'];
  7. }
  8. ?>
the changes i made from previous code is only line 7 where i write "echo $row['SUBSTR(short_description,1,50)']; instead of "echo $row[short_description'];"

this idea came in to mind when i ran this
SELECT SUBSTR(short_description,1,50) FROM news_contents LIMIT 1
query from phpmyadmin and it worked ,

thanx again ronald for your sincer coopration ,atlast my this thread has been solved , and i am sure that i will learn more n more from this form ..
Oct 22 '06 #8
ronverdonk
4,258 Expert 4TB
You would make it a lot easier on yourself and the viewers of the resultset if myou use an alias for the MySQL substring result, such as 'description'. Your code would then be:[PHP]<?php
include ("config.php");
$result = mysql_query("SELECT SUBSTR(short_description,1,50)
AS description FROM news_contents")
or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
echo $row['description'];
}
?>[/PHP]

Ronald :cool:
Oct 22 '06 #9
You would make it a lot easier on yourself and the viewers of the resultset if myou use an alias for the MySQL substring result, such as 'description'. Your code would then be:[PHP]<?php
include ("config.php");
$result = mysql_query("SELECT SUBSTR(short_description,1,50)
AS description FROM news_contents")
or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
echo $row['description'];
}
?>[/PHP]


Ronald :cool:
this is a great idea ronverdonk thanxs again .........................
Oct 23 '06 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Albretch | last post by:
I am trying to insert some textual data belonging to an HTML page into a table column with 'TEXT' as data type mysql's maual _/manual.html#String_types tell you, you may insert up to (2^16 - 1),...
18
by: Michael Skind | last post by:
Hello, I use a simple Table : <TABLE> <TR 1> <TD></TD> </TR> <TR 2> <TD></TD> </TR>
44
by: Mariusz Jedrzejewski | last post by:
Hi, I'll be very grateful if somebody can explain me why my Opera 7.23 (runing under linux) doesn't show me inner tables. Using below code I can see only "inner table 1". There is no problem with...
4
by: Peter Scott | last post by:
I created a table that has a column in that needs to contain a full Unix file path. Since 2048 was too long for a VARCHAR, I made it TEXT. I since populated the table. Now I want to make the...
5
by: cover | last post by:
After a while of deleting records in a MySQL db, there gets to be the gaps in the id numbering system. i.e. 1, 2, 3, 6, 7, 12, and so on. Is there a way to renumber the id system in a table for 1,...
8
by: flyingisfun1217 | last post by:
Hey, Sorry to bother everybody again, but this group seems to have quite a few knowledgeable people perusing it. Here's my most recent problem: For a small project I am doing, I need to...
1
by: Daniel Manes | last post by:
I'm baffled. I have a column in a SQL Server Express database called "Longitude," which is a float. When I view the table in a DataGridView, some of the numbers, which only have two decimal places...
4
by: Vlinder | last post by:
I'm completely new to PHP (jumped ship this week from MSAccess and ASP to MySql and PHP) and I'd appreciate some help with leading zeros in telephone numbers. I've got a field with type...
4
by: Astley Le Jasper | last post by:
I've been getting errors recently when using pysqlite. I've declared the table columns as real numbers to 2 decimal places (I'm dealing with money), but when doing division on two numbers that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.