473,407 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Mysql querie problems

The following code does not select the appropiate fields, it select
everything with in a zip code ( as an example)and so on. I have tried to
modify but I am a rookie and everything I tried has failed. Help please

$query = "Select xxx, xxx, xxx, xxx, xxx, xxx, xxx From
`list_data`
where (keyword like '$keyword%' and zip='$zip'
or key2 like '$key2%' and zip='
$zip'
or key3 like '$key3%' and zip='$zip'
or keyword like '$keyword%' and city='$city' and state='$state'
or key2 like '$key2%' and city='$city' and state='$state'
or key3 like '$key3%' and city='$city' and state='$state'
or name like '$name%' and zip='$zip'
or name like '$name%' and city='$city' and state='$state'
or keyword like '$keyword%' and city='$city' and country='$country'
or key2 like '$key2%' and city='$city' and country='$country'
or key3 like '$key3%' and city='$city' and country='$country'
or name like '$name%' and city='$ity' and country='$country'
or name like '$name%' and city='$city' and country='$country'
or name like '$name%' and city='$city' and country='$country')";
Mar 30 '06 #1
3 1554
avandenbroeck wrote:
The following code does not select the appropiate fields, it select
everything with in a zip code ( as an example)and so on. I have tried to
modify but I am a rookie and everything I tried has failed. Help please .. . .
where (keyword like '$keyword%' and zip='$zip'
or key2 like '$key2%' and zip='$zip'
or key3 like '$key3%' and zip='$zip'
or name like '$name%' and zip='$zip'


I would guess that one of the following variables are a blank string:
$keyword, $key2, $key3, $name

For instance, if $key2 is blank, then you'll have a term in your query:
key2 LIKE '%' AND zip='01234'

A LIKE predicate comparing with '%' is always true.

So print the resulting string $query after you've interpolated all your
variables into it, and look for occurrances of '%'.

Regards,
Bill K.
Mar 30 '06 #2
Bill Karwin wrote:
avandenbroeck wrote:
The following code does not select the appropiate fields, it select
everything with in a zip code ( as an example)and so on. I have tried to
modify but I am a rookie and everything I tried has failed. Help please .. . .

where (keyword like '$keyword%' and zip='$zip'
or key2 like '$key2%' and zip='$zip'
or key3 like '$key3%' and zip='$zip'
or name like '$name%' and zip='$zip'

I would guess that one of the following variables are a blank string:
$keyword, $key2, $key3, $name For instance, if $key2 is blank, then you'll have a term in your query:
key2 LIKE '%' AND zip='01234' A LIKE predicate comparing with '%' is always true. So print the resulting string $query after you've interpolated all your
variables into it, and look for occurrances of '%'. Regards,
Bill K.


I agree with Bill - also is it not logically the same to write the query
as;

where
(keyword like '$keyword%'
or key2 like '$key2%'
or key3 like '$key3%'
or name like '$name%')
and zip='$zip'

Mar 30 '06 #3
noone wrote:
I agree with Bill - also is it not logically the same to write the query
as;

where (keyword like '$keyword%' or key2 like '$key2%'
or key3 like '$key3%'
or name like '$name%')
and zip='$zip'


There is a lot of rewriting that could be done to simplify the boolean
expressions in this query. There are even three lines that are
identical. And a typo of '$ity' where it should be '$city'.

But I'm not sure if the query is being generated automatically, or if
it's structured the way it is to simplify alterations to it.

Anyway, if we want to simplify the query:

WHERE
(
zip = '$zip'
OR city = '$city' AND (state = '$state' OR country = '$country')
)
AND
(
keyword LIKE '$keyword%'
OR key2 LIKE '$key2%'
OR key3 LIKE '$key3%'
OR name LIKE '$name%'
)

That logically equivalent to the OP, and it has only one test for each
column, which may speed up the query significantly.

As a side note, avandenbroeck should make sure to understand the risks
of SQL injection:
http://en.wikipedia.org/wiki/Sql_injection

Regards,
Bill K.
Mar 30 '06 #4

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

Similar topics

0
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...
0
by: Stefan Hinz | last post by:
Degan, jumping in to try and solve some problems that look pretty obvious to me ... > #options for default service (mysqld2) > (mysqld2) It should be , not (mysqld2).
0
by: Anil Garg | last post by:
Hi, I am trying to install myql on redhat. mysql package used: mysql-3.23.52-unknown-freebsdelf4.6-i386.tar.gz While following the instructions in install document..when i run the follwing...
0
by: Johannes B. Ullrich | last post by:
--=-WKgoK98ejo9BZyGYc3N/ Content-Type: text/plain Content-Transfer-Encoding: quoted-printable I am having problems with MySQL 4.0.12 on RedHat Advanced Server 2.1 using a dual Xeon with 8...
2
by: pc | last post by:
hi everyone, we have a server on which someone blew away the MySQL 4 progs directory before installing MySQL 5. When installing MySQL 5 the installation failed with an error stating that it was...
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
1
by: bcochofel | last post by:
Hi, I'm using Perl CGI and HTML::Template to generate the following XML: ---------------------------------------------------------------------- <?xml version="1.0" encoding="iso-8859-1"?>...
0
by: Paulo Ferreira | last post by:
Hello, I'm trying to run a "make-table" querie that will create a table on protected database but, every time I try to run the querie, I get a message saying that the password is wrong. The...
1
by: diatel1 | last post by:
I have a database with tables and forms. One of those table is the Inventory table, where I have fields like "product" "stock #" "buy date" "paid date" "paid yes/no", etc. I would like to have a form...
5
by: Albert Wammes | last post by:
Is it possible to create form containing an editable crosstab querie? The only way I can think of is to create a temporary table where the results of the crosstab querie are stored, then presented...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
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...

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.