MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMySQL 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 Dev Shed Forums Sponsor:
  #1  
Old October 13th, 2000, 05:43 PM
Daveg Daveg is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2000
Posts: 4 Daveg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I need some help with a PHP/mysql_query
Here's the line:

$result = mysql_query ("SELECT * FROM data1 WHERE location LIKE '$location%' AND rental LIKE '$rental%' AND purchase LIKE '$purchase%'",$db);

if ($row = mysql_fetch_array($result)) {

It mostly works, but if the user chooses a location and both rental AND purchase, it shows no records.

If the user chooses either rental OR purchase it pulls up the appropriate records correctly.

I've tried:
"AND rental LIKE '$rental%' OR purchase LIKE '$purchase%"
but it still doesn't work.

I need the search to work for users that want to choose both rental AND purchase from a location.
What am I doing wrong?

Reply With Quote
  #2  
Old October 14th, 2000, 01:03 AM
Sepodati's Avatar
Sepodati Sepodati is offline
Banned
Dev Shed God 19th Plane (14000 - 14499 posts)
 
Join Date: Dec 1999
Location: Afghanistan
Posts: 14,385 Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 73167 Folding Title: Intermediate FolderFolding Points: 73167 Folding Title: Intermediate FolderFolding Points: 73167 Folding Title: Intermediate FolderFolding Points: 73167 Folding Title: Intermediate Folder
Time spent in forums: 2 Months 4 Weeks 21 h 31 m 33 sec
Reputation Power: 1784
Send a message via ICQ to Sepodati Send a message via Yahoo to Sepodati
$result = mysql_query ("SELECT * FROM data1 WHERE location LIKE '$location%' AND ( rental LIKE '$rental%' OR purchase LIKE '$purchase%' )",$db);


Reply With Quote
  #3  
Old October 14th, 2000, 08:05 AM
Daveg Daveg is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2000
Posts: 4 Daveg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Nearly...
If a user chooses a location
And rental
And purchase
It pulls up the correct entries.

BUT
If a user chooses a location
And rental or purchase
It pulls up all fields with both rental and purchase.... which is incorrect, it should only pull up location and either rental or purchase, not both.

A simple thing maybe, but it's driving me mad!


Reply With Quote
  #4  
Old October 14th, 2000, 04:22 PM
Sepodati's Avatar
Sepodati Sepodati is offline
Banned
Dev Shed God 19th Plane (14000 - 14499 posts)
 
Join Date: Dec 1999
Location: Afghanistan
Posts: 14,385 Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 73167 Folding Title: Intermediate FolderFolding Points: 73167 Folding Title: Intermediate FolderFolding Points: 73167 Folding Title: Intermediate FolderFolding Points: 73167 Folding Title: Intermediate Folder
Time spent in forums: 2 Months 4 Weeks 21 h 31 m 33 sec
Reputation Power: 1784
Send a message via ICQ to Sepodati Send a message via Yahoo to Sepodati
Okay..you have to construct some logic to create your query. If they didn't enter a rental, then you don't want it in your query. Something like this...
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
$query = "SELECT * FROM data1 WHERE location LIKE '%$location%' ";
if ($rental != "")
{
$query .= " AND rental LIKE '%$rental' ";
}
if ($purchase != "")
{
$query .= " AND purchase LIKE '%$purchase%' ";
}

$result = mysql_query($query,$db);
[/code]
The problem was if the user didn't enter rental, then $rental eithe wasn't set or equaled an empty string. So when you search for something like

rental LIKE '%$rental%'

You're actually searching for something like

rental LIKE '%%'

which will return all of the records.

---John Holmes...

[This message has been edited by SepodatiCreations (edited October 14, 2000).]

Reply With Quote
  #5  
Old October 14th, 2000, 05:58 PM
Daveg Daveg is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2000
Posts: 4 Daveg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
But if a user chooses rental AND purchase.

There's no records found, because there are no records that are both... they are either one or the other.

What I need is that when the user chooses both, they get records that have the chosen location AND are both rental and purchase.

I hope this makes sense, I appreciate your help!

Reply With Quote
  #6  
Old October 14th, 2000, 07:14 PM
JohnGray JohnGray is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 1999
Location: euless, texas, usa
Posts: 3 JohnGray User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Same concept, just merging the two posted answers above. I haven't tested this at all, so caveat emptor.

<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>$query = "SELECT * FROM data1 WHERE location LIKE '$location%'";

if ($rental != "" && $purchase != "") {
$query .= " AND (rental LIKE '$rental%' OR purchase LIKE '$purchase%')";
} elseif ($rental != "" && $purchase == "") {
$query .= " AND rental LIKE '$rental%'";
} elseif ($rental == "" && purchase != "") {
$query .= " AND purchase LIKE '$purchase%'";
}

$result = mysql_query($query,$db);[/code]

Reply With Quote
  #7  
Old October 15th, 2000, 11:49 AM
Daveg Daveg is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2000
Posts: 4 Daveg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks John, that's exactly what I was looking for.

It works as I wanted.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > mysql_query question


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 2 hosted by Hostway
Stay green...Green IT