Solved

Selecting all data from the related category

Posted on 2016-11-13
3
13 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 31

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
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.

932 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

12 Experts available now in Live!

Get 1:1 Help Now