SELECT query with multiple "WHERE" Clause

SELECT query with multiple "WHERE" Clause

am 28.02.2008 00:44:23 von Nasreen Laghari

--0-1008320802-1204155863=:17450
Content-Type: text/plain; charset=us-ascii

Hi All,

Thank you for increasing my knowledge about PHP/MYSQL.

I am creating a SEARCH, by only using one table. The search form is same as Inserting item (search has form of all fields in table ), difference is SEARCH page doesnt have validation . Therefore user can enter information in any of field. I would like to know how to write a SELECT query which has multiple where clause with OR operator.

shall we write:

$query = mysql_query("SELECT * from gig WHERE Name='$name' || WHERE gig_fdate='$sdate'");

OR

$query = mysql_query("SELECT * from gig WHERE gigName='$gig_name' OR WHERE gig_fdate='$sdate'");

OR

$query = mysql_query("SELECT * from gig WHERE gigName='$gig_name' || gig_fdate='$sdate'");


Regards

Nasreen


____________________________________________________________ ________________________
Looking for last minute shopping deals?
Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newsearch/category.php?categor y=shopping
--0-1008320802-1204155863=:17450--

Re: SELECT query with multiple "WHERE" Clause

am 28.02.2008 00:59:42 von parasane

On Wed, Feb 27, 2008 at 6:44 PM, Nasreen Laghari
wrote:
> I am creating a SEARCH, by only using one table. The search form is same as Inserting item (search has form of all fields in table ), difference is SEARCH page doesnt have validation . Therefore user can enter information in any of field. I would like to know how to write a SELECT query which has multiple where clause with OR operator.

SELECT * FROM tableName WHERE (colA LIKE '%value%' OR colB='1');

--- more ---

SELECT fieldA,fieldR,fieldT,fieldX FROM tableName WHERE
(colA='value' OR colB LIKE 'Hello%') AND colC='Active';

--


Daniel P. Brown
Senior Unix Geek


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: SELECT query with multiple "WHERE" Clause

am 28.02.2008 01:01:05 von Stut

On 27 Feb 2008, at 23:44, Nasreen Laghari wrote:
> Thank you for increasing my knowledge about PHP/MYSQL.

The question you ask below is basic SQL syntax. Please read the MySQL
manual before asking here - answers at this level are all in there.

http://mysql.com/doc

Oh, and once you have it working try entering

';delete * from gig;select * from gig where Name='

(including quotes) into the gig_name form field. When you get over the
loss of all your data go read about sanitising your input: http://php.net/mysql_real_escape_string

-Stut

--
http://stut.net/

>
> I am creating a SEARCH, by only using one table. The search form is
> same as Inserting item (search has form of all fields in table ),
> difference is SEARCH page doesnt have validation . Therefore user
> can enter information in any of field. I would like to know how to
> write a SELECT query which has multiple where clause with OR operator.
>
> shall we write:
>
> $query = mysql_query("SELECT * from gig WHERE Name='$name' || WHERE
> gig_fdate='$sdate'");
>
> OR
>
> $query = mysql_query("SELECT * from gig WHERE gigName='$gig_name' OR
> WHERE gig_fdate='$sdate'");
>
> OR
>
> $query = mysql_query("SELECT * from gig WHERE gigName='$gig_name'
> || gig_fdate='$sdate'");
>
>
> Regards
>
> Nasreen
>
>
>
> ____________________________________________________________ ________________________
> Looking for last minute shopping deals?
> Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newsearch/category.php?categor y=shopping

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: SELECT query with multiple "WHERE" Clause

am 28.02.2008 01:03:05 von Stephen Johnson

$query =3D mysql_query("SELECT * from gig WHERE gigName=3D'$gig_name' or
gig_fdate=3D'$sdate'");

You can not use more then one WHERE in your sql statement... And SQL accept=
s
OR and AND.. =20


--
Stephen Johnson c | eh
The Lone Coder

http://www.thelonecoder.com
continuing the struggle against bad code

http://www.fortheloveofgeeks.com
I=B9m a geek and I=B9m OK!
--




> From: Nasreen Laghari
> Date: Wed, 27 Feb 2008 15:44:23 -0800 (PST)
> To:
> Subject: [PHP-DB] SELECT query with multiple "WHERE" Clause
>=20
>=20
>=20
> $query =3D mysql_query("SELECT * from gig WHERE gigName=3D'$gig_name' ||
> gig_fdate=3D'$sdate'");

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: SELECT query with multiple "WHERE" Clause

am 28.02.2008 01:04:49 von dmagick

> $query = mysql_query("SELECT * from gig WHERE gigName='$gig_name' OR WHERE gig_fdate='$sdate'");

This one.

I'd suggest you get a book to help you with the basics, something like
this should do (first hit in amazon, haven't actually read this
particular book):

http://www.amazon.com/Learning-MySQL-Seyed-Saied-Tahaghoghi/ dp/0596008643/

There's lots of stuff to learn in sql.

--
Postgresql & php tutorials
http://www.designmagick.com/

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: SELECT query with multiple "WHERE" Clause

am 28.02.2008 01:06:08 von Trevor Gryffyn

$query = mysql_query("SELECT * FROM gig WHERE gigName='$gig_name' OR
gig_fdate='$sdate'");

You only use the WHERE clause once then use parenthesis, AND and OR to create
the logical conditions.

If you have access to the mysql server, maybe through phpMyAdmin or
something, I'd highly recommend forming your SQL statements using that,
then creating your PHP once you've perfected your SQL.

SQL statements can be very powerful and sometimes dangerous and it's much
easier to debug the SQL when you work with it by itself and not have to
worry about any PHP issues too.

Assuming your MySQL server is on another server, if you have a Windows
machine you can use a program like WinSQL Lite or Navicat to connection to
the MySQL server (if it allows remote connections).

phpMyAdmin is probably the easiest option though.

-TG

----- Original Message -----
From: Nasreen Laghari
To: php-db@lists.php.net
Date: Wed, 27 Feb 2008 15:44:23 -0800 (PST)
Subject: [PHP-DB] SELECT query with multiple "WHERE" Clause

> Hi All,
>
> Thank you for increasing my knowledge about PHP/MYSQL.
>
> I am creating a SEARCH, by only using one table. The search form is same
as Inserting item (search has form of all fields in table ), difference is
SEARCH page doesnt have validation . Therefore user can enter information
in any of field. I would like to know how to write a SELECT query which has
multiple where clause with OR operator.
>
> shall we write:
>
> $query = mysql_query("SELECT * from gig WHERE Name='$name' || WHERE
gig_fdate='$sdate'");
>
> OR
>
> $query = mysql_query("SELECT * from gig WHERE gigName='$gig_name' OR WHERE
gig_fdate='$sdate'");
>
> OR
>
> $query = mysql_query("SELECT * from gig WHERE gigName='$gig_name' ||
gig_fdate='$sdate'");
>
>
> Regards
>
> Nasreen

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: SELECT query with multiple "WHERE" Clause

am 28.02.2008 01:08:40 von Greg Bowser

------=_Part_16384_6521682.1204157320209
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

In MySQL, both "OR" and "||" are valid logical or operators. You can only
have one Where clause, thus your last example is correct.

--GREG

On Wed, Feb 27, 2008 at 6:44 PM, Nasreen Laghari
wrote:

> Hi All,
>
> Thank you for increasing my knowledge about PHP/MYSQL.
>
> I am creating a SEARCH, by only using one table. The search form is same
> as Inserting item (search has form of all fields in table ), difference is
> SEARCH page doesnt have validation . Therefore user can enter information in
> any of field. I would like to know how to write a SELECT query which has
> multiple where clause with OR operator.
>
> shall we write:
>
> $query = mysql_query("SELECT * from gig WHERE Name='$name' || WHERE
> gig_fdate='$sdate'");
>
> OR
>
> $query = mysql_query("SELECT * from gig WHERE gigName='$gig_name' OR WHERE
> gig_fdate='$sdate'");
>
> OR
>
> $query = mysql_query("SELECT * from gig WHERE gigName='$gig_name' ||
> gig_fdate='$sdate'");
>
>
> Regards
>
> Nasreen
>
>
>
> ____________________________________________________________ ________________________
> Looking for last minute shopping deals?
> Find them fast with Yahoo! Search.
> http://tools.search.yahoo.com/newsearch/category.php?categor y=shopping
>

------=_Part_16384_6521682.1204157320209--

Re: SELECT query with multiple "WHERE" Clause

am 28.02.2008 01:14:29 von dmagick

Greg Bowser wrote:
> In MySQL, both "OR" and "||" are valid logical or operators. You can only
> have one Where clause, thus your last example is correct.

Though in postgresql and db2 (and some other dbs) "||" means
"concatenate" so stick with using the word "OR" in this situation
otherwise you'll run into portability issues if you ever needed to move
to another db.

--
Postgresql & php tutorials
http://www.designmagick.com/

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php