Database Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsOther TechnologiesDatabase Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old November 12th, 2003, 09:13 PM
Dicky Dicky is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 99 Dicky User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
MySQL where wildcard

I am running a select query as follows:
php Code:
Original - php Code
  1.  
  2. $result = mysql_query("SELECT count(*) as count FROM diary WHERE category = '$cat'",$db);

The $cat variable will hold a user selected category. The problem I have is if no category has been selected what can I use as a wild card such as * to select all the records?

This MUST be loaded to the $cat variable and not be a change in the SQL statement.

Any help much appreciated.

Reply With Quote
  #2  
Old November 12th, 2003, 11:21 PM
honcho's Avatar
honcho honcho is offline
Contributing User
Codewalkers Beginner (1000 - 1499 posts)
 
Join Date: Apr 2007
Location: Cape Cod
Posts: 1,347 honcho User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 52 m 2 sec
Reputation Power: 3
RE: MySQL where wildcard

There are two wildcard characters in MySQL: _ and %

_ (underscore) takes the places of exactly one character.
% takes the places of 0 or more characters.
(If you use wildcards, you need to use LIKE as the comparison operator instead of = )

So, category LIKE 'c?t' will match cat, cot, cut
and category LIKE 'p%t' will match pt, pat, pet, pit, past, etc.

Reply With Quote
  #3  
Old November 13th, 2003, 03:51 AM
nazly nazly is offline
Codewalkers Beginner (1000 - 1499 posts)
 
Join Date: Apr 2007
Location: Colombo,SriLanka
Posts: 1,325 nazly User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 18 sec
Reputation Power: 3
Send a message via Yahoo to nazly
RE: MySQL where wildcard

Simply for your case you can do something else like this..

php Code:
Original - php Code
  1.  
  2. $query="SELECT count(*) as count FROM diary";
  3. if(isset($cat))$query.=" WHERE category = '$cat'";
  4. $result = mysql_query($query,$db);



Quote:
This MUST be loaded to the $cat variable and not be a change in the SQL statement.

But I'm not sure why you have mentioned this..

Reply With Quote
  #4  
Old November 13th, 2003, 12:46 PM
Dicky Dicky is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 99 Dicky User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
RE: MySQL where wildcard

Thanks for your help.

I am already using the option you have suggested Nazly. I was hoping for something bit tidier but it looks like it does not exist - maybe one day.

Reply With Quote
Reply

Viewing: Codewalkers ForumsOther TechnologiesDatabase Help > MySQL where wildcard


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway