Solved

Selecting all data from the related category

Posted on 2016-11-13
3
29 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
[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
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 110

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

740 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