Solved

Selecting all data from the related category

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to import sql database into mysql workbench 18 36
comma true 6 34
INDEX does not make a difference, why? 10 52
MySQL Backup Strategy 15 28
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

832 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