Jump to content


Photo

trouble with WHERE query


  • Please log in to reply
11 replies to this topic

#1 longhorn14

longhorn14
  • Members
  • Pip
  • Newbie
  • 7 posts

Posted 30 August 2003 - 07:30 AM

I am missing something here. I would like a query where I can have records displayed according to two different criteria. I can get it done with just one, such as
[php:1:ceb2ecabf9]<?php

$query=\"SELECT * FROM customers WHERE cust_type=\'$cust_type\'\";

?>[/php:1:ceb2ecabf9]
but, when I add another field, it displays nothing and I get no errors. Ideally, I would like to have the URL indicate what I want displayed (www.somedomain.com/file.php?cust_type=5&cust_subtype=Other). A more comprehensive code snippet is below. Thanks a ton,
nat

[php:1:ceb2ecabf9]<?php
<?
include(\"dbcustomers.inc.php\");
mysql_connect(mysql,$username,$password);
@mysql_select_db($database) or die( \"Unable to select database\");
$query=\"SELECT * FROM customers WHERE cust_type=\'$cust_type\' AND WHERE cust_subtype=\'$cust_subtype\'\";

$result=mysql_query($query);



mysql_close();
$ctype=\"contacts\";
echo \"<b><center>Customer Database</center></b><br><br>\";

?>
?>[/php:1:ceb2ecabf9]

#2 Jan

Jan
  • Members
  • Pip
  • Newbie
  • 4 posts

Posted 30 August 2003 - 07:42 AM

try to only use the first WHERE and delete the second one.

... WHERE bla = \'$bla\' AND bla2 = \'$bla2\'

no second \"WHERE\"

#3 longhorn14

longhorn14
  • Members
  • Pip
  • Newbie
  • 7 posts

Posted 30 August 2003 - 05:16 PM

Thanks, but that didn\'t seem to fix it. I try to load:
http://www.racksandf...t_subtype=Other

but no records are returned and there is no entry in my log indicating an error. Any other ideas?
Thanks for trying,
nat

#4 nysebamse

nysebamse
  • Members
  • PipPip
  • Member
  • 16 posts
  • LocationBergen, Norway

Posted 03 September 2003 - 03:51 PM

Hey Nat

Do you want to make to mysql query act accordingly to the url or the browser to go to an url after the query?
Go get Firefox![br][br]http://www.mozilla.org/

#5 longhorn14

longhorn14
  • Members
  • Pip
  • Newbie
  • 7 posts

Posted 03 September 2003 - 05:39 PM

I am not sure I understand the difference. I think I would like the query to be run using the vars from the url. I would be interested in hearing about the other way, too. Thank you for asking!

#6 nysebamse

nysebamse
  • Members
  • PipPip
  • Member
  • 16 posts
  • LocationBergen, Norway

Posted 03 September 2003 - 06:00 PM

Hey

Say someone goes to this url: www.somedomain.com/file.php?cust_type=5&cust_subtype=Other

the query could act according to the url like this:
$query="SELECT * FROM customers WHERE cust_type=\'".$_GET[\'cust_type\']."\' AND cust_subtype=\'".$_GET[\'cust_subtype\']."\'";

the $_GET fetches the GET value from the url (?variable=value)
Go get Firefox![br][br]http://www.mozilla.org/

#7 longhorn14

longhorn14
  • Members
  • Pip
  • Newbie
  • 7 posts

Posted 04 September 2003 - 04:33 AM

Awesome! Thank you so much. How many of those AND statements can I have? Thank you so much for your help.

#8 nysebamse

nysebamse
  • Members
  • PipPip
  • Member
  • 16 posts
  • LocationBergen, Norway

Posted 04 September 2003 - 06:40 AM

hey

Im not sure actually. Never used more than one AND myself, by try it out and let me know :)
Go get Firefox![br][br]http://www.mozilla.org/

#9 longhorn14

longhorn14
  • Members
  • Pip
  • Newbie
  • 7 posts

Posted 05 September 2003 - 03:35 AM

Well, I tried it with two \"AND\" statements in the query and it worked fine.
Here was the query:
[php:1:584218dfa6]<?php
$query=\"SELECT * FROM customers WHERE state_res=\'\".$_GET[\'state_res\'].\"\' AND cust_type=\'\".$_GET[\'cust_type\'].\"\' AND cust_subtype=\'\".$_GET[\'cust_subtype\'].\"\'\";
?>[/php:1:584218dfa6]

The URL for this is:
http://www.racksandf.....fishing guide

Thanks for the help!

#10 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 06 September 2003 - 08:03 AM

How many of those AND statements can I have?


Generally, as many as there are columns in your table. Although if you are range checking you may have a need for several ANDs for a single column.

eg
... WHERE ((age >= 18) AND (age <= 30)) OR ((age > 65) AND (age < 70)) ...
hth
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#11 longhorn14

longhorn14
  • Members
  • Pip
  • Newbie
  • 7 posts

Posted 06 September 2003 - 07:27 PM

Good point Barand, thanks!

#12 longhorn14

longhorn14
  • Members
  • Pip
  • Newbie
  • 7 posts

Posted 07 September 2003 - 11:23 PM

try to only use the first WHERE and delete the second one.

... WHERE bla = \'$bla\' AND bla2 = \'$bla2\'

no second \\\"WHERE\\\"


Thanks Jan, you all have taught me a lot!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users