Solved

MySQL Syntax for Limit Characters

Posted on 2007-11-30
10
574 Views
Last Modified: 2008-02-01
How do I alter the following SQL Syntact to limit only 25 characters being returned?

SELECT distinct program FROM course_Finder ORDER BY program ASC
0
Comment
Question by:pda4me
10 Comments
 
LVL 20

Expert Comment

by:steelseth12
ID: 20382422
SELECT distinct program FROM course_Finder ORDER BY program ASC LIMIT 0,25
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20382429
you mean 25 characters of the column program?

SELECT distinct substr(program,1,25) FROM course_Finder ORDER BY substr(program,1,25) ASC
0
 
LVL 10

Expert Comment

by:dis1931
ID: 20382434
Hello pda4me,

Try:
SELECT distinct LEFT(program,25) FROM course_Finder ORDER BY program ASC

Regards,

dis1931
0
 
LVL 20

Expert Comment

by:Gawai
ID: 20382477
you can use a keyword LIMIT
0
 

Author Comment

by:pda4me
ID: 20382608
None of those seem to be doing the trick?  Here is the full code, what am I doing wrong?

mysql_select_db($database_memLook, $memLook);
$query_Program = "SELECT distinct program FROM course_Finder ORDER BY program ASC";
$Program = mysql_query($query_Program, $memLook) or die(mysql_error());
$row_Program = mysql_fetch_assoc($Program);
$totalRows_Program = mysql_num_rows($Program);
0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 

Author Comment

by:pda4me
ID: 20382632
I am using this to populate a select dropdown form element...is it possible to just limit the characters on the form element?

<select name="program" class="search">
         <option value="">-- sort by --</option>
         <?php
do {  
?>
         <option value="<?php echo $row_Program['program']?>"><?php echo $row_Program['program']?></option>
         <?php
} while ($row_Program = mysql_fetch_assoc($Program));
  $rows = mysql_num_rows($Program);
  if($rows > 0) {
      mysql_data_seek($Program, 0);
        $row_Program = mysql_fetch_assoc($Program);
  }
?>
            </select>
0
 
LVL 21

Accepted Solution

by:
nizsmo earned 500 total points
ID: 20385892
You can limit the characters to 25 max like this (in PHP):
<select name="program" class="search">

         <option value="">-- sort by --</option>

         <?php

do {  

?>

         <option value="<?php echo $row_Program['program']; ?>"><?php echo substr($row_Program['program'],0,25); ?></option>

         <?php

} while ($row_Program = mysql_fetch_assoc($Program));

  $rows = mysql_num_rows($Program);

  if($rows > 0) {

      mysql_data_seek($Program, 0);

        $row_Program = mysql_fetch_assoc($Program);

  }

?>

            </select>

            

            

Open in new window

0
 
LVL 21

Expert Comment

by:nizsmo
ID: 20385898
I didn't do it for your value attribute of your options tag, since I was unsure if you want to limit the string to 25 characters there. If you do, it is exactly the same procedure.
0
 

Author Comment

by:pda4me
ID: 20386562
Thanks Nizsmo!
0
 
LVL 21

Expert Comment

by:nizsmo
ID: 20386953
glad to be of assistance :)
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem sending file attachments 8 23
sql_mode 1 19
geting geocode of a user with using Google maps api 8 18
Wordpress errors showing up, hacked? 4 12
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

895 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

13 Experts available now in Live!

Get 1:1 Help Now