473,763 Members | 1,882 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

mysql query - select any

Hi guys,

I have the following sql statemant to search a mysql database that gets
if values from a form with combo box's in.

SELECT * FROM hottubs, manufacturers WHERE manufacturers.m anid =
hottubs.manid AND hottubs.type = '%s' AND hottubs.dimleng th <= '%s' AND
hottubs.dimwidt h <= '%s' AND hottubs.dimhigh t <= '%s' AND
hottubs.seatsto <= '%s' AND hottubs.shape = '%s' ORDER BY
$thesearchtype_ search.

Everything works fine except I want to add a select "any" from the
shape combo box. I really need a way of cutting out the last " AND
hottubs.shape = '%s' " if the $_GET['shape'] = 'any'
Will this work using a variable as shown below??

IF ($_GET['shape'] != 'any' )
{
$shape = AND hottubs.shape = '%s'
}

SELECT * FROM hottubs, manufacturers WHERE manufacturers.m anid =
hottubs.manid AND hottubs.type = '%s' AND hottubs.dimleng th <= '%s' AND
hottubs.dimwidt h <= '%s' AND hottubs.dimhigh t <= '%s' AND
hottubs.seatsto <= '%s' $shape ORDER BY $thesearchtype_ search

I have only been doing php about a month so go gentle!

Alex

Dec 3 '06 #1
6 2044
alex.kemsley wrote :
Hi guys,

I have the following sql statemant to search a mysql database that gets
if values from a form with combo box's in.

SELECT * FROM hottubs, manufacturers WHERE manufacturers.m anid =
hottubs.manid AND hottubs.type = '%s' AND hottubs.dimleng th <= '%s' AND
hottubs.dimwidt h <= '%s' AND hottubs.dimhigh t <= '%s' AND
hottubs.seatsto <= '%s' AND hottubs.shape = '%s' ORDER BY
$thesearchtype_ search.

Everything works fine except I want to add a select "any" from the
shape combo box. I really need a way of cutting out the last " AND
hottubs.shape = '%s' " if the $_GET['shape'] = 'any'
Will this work using a variable as shown below??

IF ($_GET['shape'] != 'any' )
{
$shape = AND hottubs.shape = '%s'
}

SELECT * FROM hottubs, manufacturers WHERE manufacturers.m anid =
hottubs.manid AND hottubs.type = '%s' AND hottubs.dimleng th <= '%s' AND
hottubs.dimwidt h <= '%s' AND hottubs.dimhigh t <= '%s' AND
hottubs.seatsto <= '%s' $shape ORDER BY $thesearchtype_ search

I have only been doing php about a month so go gentle!

Alex
Well, I just looked a bit, that should work.
But you know, the best way to know if it works is to test it yourself :)

--
Naixn
http://fma-fr.net
Dec 3 '06 #2
alex.kemsley wrote:
Hi guys,

I have the following sql statemant to search a mysql database that gets
if values from a form with combo box's in.

SELECT * FROM hottubs, manufacturers WHERE manufacturers.m anid =
hottubs.manid AND hottubs.type = '%s' AND hottubs.dimleng th <= '%s' AND
hottubs.dimwidt h <= '%s' AND hottubs.dimhigh t <= '%s' AND
hottubs.seatsto <= '%s' AND hottubs.shape = '%s' ORDER BY
$thesearchtype_ search.

Everything works fine except I want to add a select "any" from the
shape combo box. I really need a way of cutting out the last " AND
hottubs.shape = '%s' " if the $_GET['shape'] = 'any'
Will this work using a variable as shown below??

IF ($_GET['shape'] != 'any' )
{
$shape = AND hottubs.shape = '%s'
}

SELECT * FROM hottubs, manufacturers WHERE manufacturers.m anid =
hottubs.manid AND hottubs.type = '%s' AND hottubs.dimleng th <= '%s' AND
hottubs.dimwidt h <= '%s' AND hottubs.dimhigh t <= '%s' AND
hottubs.seatsto <= '%s' $shape ORDER BY $thesearchtype_ search

I have only been doing php about a month so go gentle!

Alex
I don't really see how this works when using %s in multiple places.
Personally I like to construct the querystring as a variable using
conditional logic in php, then pass the variable to MySQL. Here's an
example since I'm a little unclear like I said...

function getNotes($tNote ID, $debugMode, $orderBy, $noteTypeID) {
$tNoteID = ( isset($tNoteID) ? $tNoteID : 0 );
$noteTypeID = ( isset($noteType ID) ? $noteTypeID : 0 );
$queryStr = "
SELECT
n.noteID
, n.noteTitle
, n.noteURL
, n.noteText
, n.createDate
, DATE_FORMAT(n.c reateDate, '%d-%b-%Y') as fmtCreateDate
, n.updateDate
, DATE_FORMAT(n.u pdateDate, '%d-%b-%Y') as fmtupdateDate
, n.noteTypeID
, nt.noteType
FROM
notes n
LEFT JOIN noteTypes nt ON n.noteTypeID = nt.noteTypeID
WHERE
0=0";
if ( $noteTypeID 0) {
$queryStr = $queryStr." AND n.noteTypeID = ".$noteType ID;
}

if ( $tNoteID 0) {
$queryStr = $queryStr." AND n.noteID = ".$tNoteID;
}

$orderBy = ( strlen($orderBy ) 0 ? $orderBy : "nt.noteTyp e,
n.noteTitle desc");
$queryStr = $queryStr." ORDER BY ".$orderBy;

if ( $debugMode ) {
echo "<br>qryDtl : ".$queryStr."<b r>";
}
return mysql_query($qu eryStr);

Dec 4 '06 #3
alex.kemsley wrote:
Hi guys,

I have the following sql statemant to search a mysql database that gets
if values from a form with combo box's in.

SELECT * FROM hottubs, manufacturers WHERE manufacturers.m anid =
hottubs.manid AND hottubs.type = '%s' AND hottubs.dimleng th <= '%s' AND
hottubs.dimwidt h <= '%s' AND hottubs.dimhigh t <= '%s' AND
hottubs.seatsto <= '%s' AND hottubs.shape = '%s' ORDER BY
$thesearchtype_ search.

Everything works fine except I want to add a select "any" from the
shape combo box. I really need a way of cutting out the last " AND
hottubs.shape = '%s' " if the $_GET['shape'] = 'any'
Will this work using a variable as shown below??

IF ($_GET['shape'] != 'any' )
{
$shape = AND hottubs.shape = '%s'
}

SELECT * FROM hottubs, manufacturers WHERE manufacturers.m anid =
hottubs.manid AND hottubs.type = '%s' AND hottubs.dimleng th <= '%s' AND
hottubs.dimwidt h <= '%s' AND hottubs.dimhigh t <= '%s' AND
hottubs.seatsto <= '%s' $shape ORDER BY $thesearchtype_ search

I have only been doing php about a month so go gentle!

Alex
Personally, I don't like building the where clause for a query all in
one go like this. I also don't even like building the clause with if's
and string concatenation. To get the cleanest, easiest where clause
construction, I typically use an array and implode it at the end.
Since I usually need to juggle a half-dozen or more conditions which
may or may not be used in a particular query, this method lets me very
easily build up which conditions I need set.

So I would do your example in a manner like this:

$where = array();
$where[] = "manufacturers. manid = hottubs.manid";
$where[] = "hottubs.ty pe = '%s'";
$where[] = "hottubs.dimlen gth <= '%s'";
$where[] = "hottubs.dimwid th <= '%s'";
$where[] = "hottubs.dimhig ht <= '%s'";
$where[] = "hottubs.seatst o <= '%s'";
IF ($_GET['shape'] != 'any' ) $where[] = "hottubs.sh ape = '%s'";

$strWhere = '';
if (count($where) 0) $strWhere = " WHERE " . implode(' AND ',
$where);

$qry = "SELECT *
FROM hottubs, manufacturers
" . $strWhere . "
ORDER BY " . $thesearchtype_ search;
- Moot

Dec 4 '06 #4

Moot wrote:
alex.kemsley wrote:
Hi guys,

I have the following sql statemant to search a mysql database that gets
if values from a form with combo box's in.

SELECT * FROM hottubs, manufacturers WHERE manufacturers.m anid =
hottubs.manid AND hottubs.type = '%s' AND hottubs.dimleng th <= '%s' AND
hottubs.dimwidt h <= '%s' AND hottubs.dimhigh t <= '%s' AND
hottubs.seatsto <= '%s' AND hottubs.shape = '%s' ORDER BY
$thesearchtype_ search.

Everything works fine except I want to add a select "any" from the
shape combo box. I really need a way of cutting out the last " AND
hottubs.shape = '%s' " if the $_GET['shape'] = 'any'
Will this work using a variable as shown below??

IF ($_GET['shape'] != 'any' )
{
$shape = AND hottubs.shape = '%s'
}

SELECT * FROM hottubs, manufacturers WHERE manufacturers.m anid =
hottubs.manid AND hottubs.type = '%s' AND hottubs.dimleng th <= '%s' AND
hottubs.dimwidt h <= '%s' AND hottubs.dimhigh t <= '%s' AND
hottubs.seatsto <= '%s' $shape ORDER BY $thesearchtype_ search

I have only been doing php about a month so go gentle!

Alex

Personally, I don't like building the where clause for a query all in
one go like this. I also don't even like building the clause with if's
and string concatenation. To get the cleanest, easiest where clause
construction, I typically use an array and implode it at the end.
Since I usually need to juggle a half-dozen or more conditions which
may or may not be used in a particular query, this method lets me very
easily build up which conditions I need set.

So I would do your example in a manner like this:

$where = array();
$where[] = "manufacturers. manid = hottubs.manid";
$where[] = "hottubs.ty pe = '%s'";
$where[] = "hottubs.dimlen gth <= '%s'";
$where[] = "hottubs.dimwid th <= '%s'";
$where[] = "hottubs.dimhig ht <= '%s'";
$where[] = "hottubs.seatst o <= '%s'";
IF ($_GET['shape'] != 'any' ) $where[] = "hottubs.sh ape = '%s'";

$strWhere = '';
if (count($where) 0) $strWhere = " WHERE " . implode(' AND ',
$where);

$qry = "SELECT *
FROM hottubs, manufacturers
" . $strWhere . "
ORDER BY " . $thesearchtype_ search;
- Moot
Thanks moot,
I like your style its very neat.
Alex

Dec 4 '06 #5
Moot wrote :
>
Personally, I don't like building the where clause for a query all in
one go like this. I also don't even like building the clause with if's
and string concatenation. To get the cleanest, easiest where clause
construction, I typically use an array and implode it at the end.
Since I usually need to juggle a half-dozen or more conditions which
may or may not be used in a particular query, this method lets me very
easily build up which conditions I need set.

So I would do your example in a manner like this:

$where = array();
$where[] = "manufacturers. manid = hottubs.manid";
$where[] = "hottubs.ty pe = '%s'";
$where[] = "hottubs.dimlen gth <= '%s'";
$where[] = "hottubs.dimwid th <= '%s'";
$where[] = "hottubs.dimhig ht <= '%s'";
$where[] = "hottubs.seatst o <= '%s'";
IF ($_GET['shape'] != 'any' ) $where[] = "hottubs.sh ape = '%s'";

$strWhere = '';
if (count($where) 0) $strWhere = " WHERE " . implode(' AND ',
$where);

$qry = "SELECT *
FROM hottubs, manufacturers
" . $strWhere . "
ORDER BY " . $thesearchtype_ search;
- Moot
In fact, that is cleaner. But the use of implode is what annoys me. When
you get a really big request, it may be, for sure, the cleanest, but surely
not the fastest... Not that it would be a great deal, but well.

But it is clean. I admit, approve, and I'll test and maybe adopt. :p

Thx.

--
Naixn
http://fma-fr.net
Dec 4 '06 #6
naixn wrote:
Moot wrote :

Personally, I don't like building the where clause for a query all in
one go like this. I also don't even like building the clause with if's
and string concatenation. To get the cleanest, easiest where clause
construction, I typically use an array and implode it at the end.
Since I usually need to juggle a half-dozen or more conditions which
may or may not be used in a particular query, this method lets me very
easily build up which conditions I need set.

So I would do your example in a manner like this:

$where = array();
$where[] = "manufacturers. manid = hottubs.manid";
$where[] = "hottubs.ty pe = '%s'";
$where[] = "hottubs.dimlen gth <= '%s'";
$where[] = "hottubs.dimwid th <= '%s'";
$where[] = "hottubs.dimhig ht <= '%s'";
$where[] = "hottubs.seatst o <= '%s'";
IF ($_GET['shape'] != 'any' ) $where[] = "hottubs.sh ape = '%s'";

$strWhere = '';
if (count($where) 0) $strWhere = " WHERE " . implode(' AND ',
$where);

$qry = "SELECT *
FROM hottubs, manufacturers
" . $strWhere . "
ORDER BY " . $thesearchtype_ search;
- Moot

In fact, that is cleaner. But the use of implode is what annoys me. When
you get a really big request, it may be, for sure, the cleanest, but surely
not the fastest... Not that it would be a great deal, but well.

But it is clean. I admit, approve, and I'll test and maybe adopt. :p

Thx.

--
Naixn
http://fma-fr.net
Agreed, Moot's code is very sleek. Although, one wonders what happens
in the event of an "or" condition. Just teasing cause you got mad
style. ;-)

Dec 5 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
6460
by: Gordon | last post by:
I have 2 tables t and t1. In this case, t1 is a copy of t. I want to delete rows from t1 based on criteria on the t table and a relationship between t ad t1 (in this case the id column). In the results below, I would think that the delete should have deleted row 1 {1 5 me) and not row 3 (1 5 they) when I run this statement delete t1 from t, t1 where t.id = t1.id and t.id=1 and t.name = 'me'; Any ideas on why row 2 is deleted?
0
3527
by: Lenz Grimmer | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, MySQL 4.0.14, a new version of the popular Open Source/Free Software Database, has been released. It is now available in source and binary form for a number of platforms from our download pages at http://www.mysql.com/downloads/ and mirror sites.
5
4012
by: red85 | last post by:
hello i have mysql 4.1 with win2000 SP3, i know that it is only an alpha and i don't know if someone else has already posted this problem: when i execute this sql UPDATE tableX SET fieldX=valueX WHERE id IN (SELECT tX.id FROM tableX AS tX WHERE tX.fieldY=valueY AND filedZ=valueZ);
0
2691
by: Philip Stoev | last post by:
Hi all, Please tell me if any of this makes sense. Any pointers to relevant projects/articles will be much appreciated. Philip Stoev http://www.stoev.org/pivot/manifest.htm ===================================
0
3948
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest version of this document can be found at: http://prdownloads.sourceforge.net/souptonuts/README_mysql.txt?download
1
3379
by: jlee | last post by:
I'm pretty much a newbie on mysql, and I need some help. I am running mysql Ver 12.22 Distrib 4.0.24, for portbld-freebsd5.4 (i386) on a server hosting an active website. The site's developer uses his own php shopping cart to receive customer orders. The configuration was done via cPanel with no external modifications - which produced no protests when built, ran and connected with no
8
2366
by: Fred | last post by:
Hello, Our website is currently developed in ASP/Mysql 4. The dedicated servers on which it is currently hosted arrive at saturation. Here is their configuration: - 1 server PIV 2,8Ghz 1GB RAM with IIS 5 on Windows 2000 - 1 server Bi-xeon 3Ghz, 512 MB with MySQL 4 on Windows 2003 The website makes approximately 10.000.000 of pages seen and 310.000
13
3424
by: Ciaran | last post by:
Hi All, Is it faster to have mySql look up as much data as possible in one complex query or to have php do all the complex processing and submit lots of simple queries to the mysql database? Cheers, Ciarán
6
38518
Atli
by: Atli | last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users. Anyone should be able to get through this without much trouble. Programming knowledge is not required. Index What is SQL? Why MySQL? Installing MySQL. Using the MySQL command line interface
0
9566
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9389
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10149
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10003
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9943
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9828
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8825
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5410
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3918
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.