Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Performing multiple mysql queries with PHP
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
Vlad
Apprentice
Apprentice


Joined: 09 Apr 2002
Posts: 264
Location: San Diego, California

PostPosted: Wed Jul 30, 2003 8:39 am    Post subject: Performing multiple mysql queries with PHP Reply with quote

Alright, I'm trying to search a database with several columns. Each column has one string from a fixed set of strings. I'm trying to allow users to submit values from a form with drop down menus. Each menu has all the possible elements of one column (so one menu per column) and the user simply selects which options to query for. I want the "default" option (unselected) to return all of the rows.

So, if a user has the options (column "Animal) [Unselected, Dog, Cat, Horse, Cow] and the options (column "Color") [Unselected, Grey, Brown, Black, White] and selects [Unselected] and [Grey], the query will return all rows for which Color=Grey.

Interestingly enough, if I were to do this from the mysql command line...
Code:

mysql>SELECT * FROM database WHERE Animal=IFNULL("Dog",Animal) AND Color=IFNULL("Grey",Color);


It would return all rows that have both Dog in the Animal column AND Grey in the Color column. Translating this to mysql is another story -

Code:

$query = "SELECT * FROM database WHERE Animal=IFNULL(\"$Animal\",Animal) AND Color=IFNULL(\"$Color\",Color)


Returns nothing.

I've debugged this by verifying that print "$Animal"; returns the correct variable the user has submitted. So if the user submits Dog, print "Animal". will print Dog. I realize what I'm doing is probably a syntax error...I've tried doing various combinations of ", ', `, to no effect. But it *does* work on the command line, so there must be a way to make it work with PHP.

Vlad
Back to top
View user's profile Send private message
Celtis
l33t
l33t


Joined: 05 Jul 2003
Posts: 737

PostPosted: Wed Jul 30, 2003 8:48 am    Post subject: Reply with quote

Just out of interest, what client API version do you get for mysql from a phpinfo(); ? I'm wondering if you have the 3.23 API's installed and they don't support a query like that.

Just a stab in the dark!
Back to top
View user's profile Send private message
Vlad
Apprentice
Apprentice


Joined: 09 Apr 2002
Posts: 264
Location: San Diego, California

PostPosted: Wed Jul 30, 2003 4:07 pm    Post subject: Reply with quote

In the mysql section it lists...

Client API version 4.0.13

And some basic server information (if it will help):
Server Version: Apache/2.0.47 (Gentoo/Linux) mod_perl/1.99_09 Perl/v5.8.0 mod_ssl/2.0.47 OpenSSL/0.9.6j PHP/4.3.2
Back to top
View user's profile Send private message
Haro
n00b
n00b


Joined: 17 May 2003
Posts: 71
Location: West Bend, WI

PostPosted: Wed Jul 30, 2003 5:13 pm    Post subject: Reply with quote

I don't think you need the dblquotes at all because the information posted from the form is already a string (obviously).

Also, it's good practice to use $_POST['formfield'] when retrieving information from a form where method="post".

Code:
$query = "SELECT * FROM `database` WHERE `Animal`=IFNULL({$_POST['Animal']},`Animal`) AND `Color`=IFNULL({$_POST['Color']},`Color`)";


Also, try printing out the query and error, if exists.

Code:
$query = "SELECT * FROM `database` WHERE `Animal`=IFNULL({$_POST['Animal']},`Animal`) AND `Color`=IFNULL({$_POST['Color']},`Color`)";

$query = mysql_query($query);

if(!$query) {
  echo $query."<br><br>."mysql_error();
}
Back to top
View user's profile Send private message
lisa
Retired Dev
Retired Dev


Joined: 01 Jun 2003
Posts: 273
Location: York, UK again! Horray!

PostPosted: Wed Jul 30, 2003 7:20 pm    Post subject: Reply with quote

SELECT .... WHERE color='Grey' AND Animal='dog' ?
_________________
Distcc guide
Visit my website
I maintain Distcc, Ccache, Memcached, and some others (i think)
Back to top
View user's profile Send private message
Vlad
Apprentice
Apprentice


Joined: 09 Apr 2002
Posts: 264
Location: San Diego, California

PostPosted: Wed Jul 30, 2003 11:37 pm    Post subject: Reply with quote

Code:

SELECT .... WHERE color='Grey' AND Animal='dog'


That's what I want to do; however; the script sometimes passes one (or multiple) null arguments from the form (ie, the default for a drop down menu) - I want these null arguments to translate as anything, except you can't do...

Code:

SELECT ... WHERE Color=* AND Animal='Dog'


since regular expressions aren't recognized without the REGEX command, and I can't figure out how to tie that into what I want to do for the life of me.

If Color is null, I want it to match ALL rows. Since only some animals in the Animal column are Dog's, it should only return animal's which are Dog's when you don't select a Color and only select an Animal.

If a user doesn't select *any* options, then all arguments return null (ie, if I do an if($argument) will return false) - but I can't match that to "anything".
Back to top
View user's profile Send private message
lisa
Retired Dev
Retired Dev


Joined: 01 Jun 2003
Posts: 273
Location: York, UK again! Horray!

PostPosted: Wed Jul 30, 2003 11:49 pm    Post subject: Reply with quote

Vlad wrote:
If Color is null, I want it to match ALL rows. Since only some animals in the Animal column are Dog's, it should only return animal's which are Dog's when you don't select a Color and only select an Animal.


So don't pass the color...


Code:

if (is_null($_POST['Color'])) {
...
   SELECT ... WHERE Animal='Dog'
...
}
else {
...
}



You should see how I build a query for a search engine. :) There's 4 or 5 functions to it based on several form inputs!
_________________
Distcc guide
Visit my website
I maintain Distcc, Ccache, Memcached, and some others (i think)
Back to top
View user's profile Send private message
Vlad
Apprentice
Apprentice


Joined: 09 Apr 2002
Posts: 264
Location: San Diego, California

PostPosted: Thu Jul 31, 2003 1:37 am    Post subject: Reply with quote

But how can I not pass one variable and still pass all the others?
Back to top
View user's profile Send private message
superjaded
l33t
l33t


Joined: 05 Jul 2002
Posts: 802

PostPosted: Thu Jul 31, 2003 2:49 am    Post subject: Reply with quote

Vlad wrote:
But how can I not pass one variable and still pass all the others?


This is usually how I set up my queries:

Code:


if ($_POST['animal'] != NULL) { $cond.=" animal='".$_POST['animal']."'";}
if ($_POST['color'] != NULL) { $cond.=($cond?' and':'')." color='".$_POST['color']."''";}
$que="select * from animals".($cond?' where '.$cond':'');


and if all went well, you could echo back the $que value and it should print something to the effect of: "select * from animals where animal='dog'"

(I hope my code was somewhat understandable :P)
Back to top
View user's profile Send private message
Vlad
Apprentice
Apprentice


Joined: 09 Apr 2002
Posts: 264
Location: San Diego, California

PostPosted: Thu Jul 31, 2003 4:56 am    Post subject: Reply with quote

It's...complicated. But will it work with, say, 7 conditions? (I don't even want to imagine how ugly that is going to be...).

Working with php/mysql has given me a new appreciation for what an absolute pain it is to create a workable database search engine....
Back to top
View user's profile Send private message
To
Veteran
Veteran


Joined: 12 Apr 2003
Posts: 1145
Location: Coimbra, Portugal

PostPosted: Thu Jul 31, 2003 9:10 am    Post subject: Reply with quote

It can work with any conditions you need/want. I'll take superjaded example and do it other way;)
Code:

if ($_POST['animal'] != NULL) { $cond.=" animal='".$_POST['animal']."' AND";}
if ($_POST['color'] != NULL) { $cond.=" color='".$_POST['color']."'' AND";}
if ($_POST['whatever'] != NULL) { $cond.=" whatever='".$_POST['whatever']."'' AND";}
$cond = substr($cond, 0, -4);
$que="select * from animals".($cond?' where '.$cond':'');

Just my way.


_________________

------------------------------------------------
Linux Gandalf 3.2.35-grsec
Gentoo Base System version 2.2
------------------------------------------------
Back to top
View user's profile Send private message
Vlad
Apprentice
Apprentice


Joined: 09 Apr 2002
Posts: 264
Location: San Diego, California

PostPosted: Thu Jul 31, 2003 9:10 pm    Post subject: Reply with quote

Tó,

Ahh, that's what I was looking for, a way to "build" the query. However, php hiccups on
Code:

$que="select * from animals".($cond?' where '.$cond':'');

with a parse error. Any idea what might be wrong? I'm going to try to debug it in the mean time...thank you for your input!
Back to top
View user's profile Send private message
Vlad
Apprentice
Apprentice


Joined: 09 Apr 2002
Posts: 264
Location: San Diego, California

PostPosted: Thu Jul 31, 2003 9:30 pm    Post subject: Reply with quote

Nevermind, I got it working perfectly using what Tó posted, minus the $que line - I rebuilt it using if statements instead of your fancy one liner =P. Thanks to everyone who offered feedback - I really appreciate all your help!

(and if you know what happened to the syntax in that $que line...feel free to respond, I'm always happy to shrink my code!)

Thanks again,
Vlad
Back to top
View user's profile Send private message
superjaded
l33t
l33t


Joined: 05 Jul 2002
Posts: 802

PostPosted: Sat Aug 02, 2003 2:53 am    Post subject: Reply with quote

Woops.

I had a stray single quote on that line, should actually read:
Code:
$que="select * from animals".($cond?' where '.$cond:'');


Notice how there's no longer a ' before the colon now.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum