Solved

Selecting all data from the related category

Posted on 2016-11-13
3
8 Views
Last Modified: 2016-11-13
Dear Experts,

I use php and mysql database

I have a drop down list where my user filter my category to see the related items.

My sql sentence is like

Select * from catagory_table where catagory='$category' order by id

I want to add to my drop down list an "all" option.

How can i write an sql to bring all the data from category table if my user selects "all" as an option.

Do i need another a coloumn in the database? Or should i use and if statement for sql statement like:

If the user selects "all" as an option, use this sql sentence otherwise use this sql sentence?
0
Comment
Question by:Braveheartli
3 Comments
 
LVL 30

Accepted Solution

by:
Marco Gasi earned 250 total points
ID: 41885164
Use an if statement:
if ($category == 'All'){
    $sql = "Select * from catagory_table order by id";
}else{
   $sql = "Select * from catagory_table where catagory='$category' order by id";
}

Open in new window

1
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 41885245
Do exactly what Marco said, but avoid SELECT * queries.  Instead, SELECT exactly the columns you need, by name.  The reason goes to issues of database performance.  And while you're thinking about performance, be sure that your database has an index on every column used in WHERE, GROUP, ORDER, HAVING, JOIN clauses.  If you do this right, you will be amazed how fast MySQL can be!
0
 
LVL 1

Author Closing Comment

by:Braveheartli
ID: 41885247
Thank you Marco, Thank you Ray
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses four methods for overlaying images in a container on a web page
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

747 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

11 Experts available now in Live!

Get 1:1 Help Now