Solved

[MySQL+PHP] SELECT -> Ignore condition if zero rows?

Posted on 2011-03-04
3
701 Views
Last Modified: 2012-05-11
Hi,

I have this example mysql_query:

mysql_query("SELECT * FROM table WHERE id='$id' AND name NOT IN($names)");

Is there any option in mysql_query to get results even if this returns Zero rows, i.e. run query ignoring that second condition?

I know it's easy to use PHP if() but I'm just curious whether is possible to do it inside one mysql_query.

Thanks for ideas!
0
Comment
Question by:Aidam-Unlimited
3 Comments
 
LVL 27

Assisted Solution

by:Lukasz Chmielewski
Lukasz Chmielewski earned 250 total points
ID: 35038083
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 35038438
you could do this:
mysql_query("SELECT * FROM table WHERE id='$id' AND ( name NOT IN($names) OR NOT EXISTS(  SELECT * FROM table WHERE id='$id' AND ( name NOT IN($names))  ))");

Open in new window

0
 

Author Closing Comment

by:Aidam-Unlimited
ID: 35069882
Thanks to both of you guys!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now