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
Solved

Selecting all data from the related category

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

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
The viewer will learn how to dynamically set the form action using jQuery.
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.

828 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