473,386 Members | 1,790 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,386 software developers and data experts.

mySQL query not fuctioning correctly

Hi,
I'm using a piece of software called 'phpcoin' along with the popular
'IPN' mod. I am having some problem with it and have got it down to an
mySQL function functioning incorrectly.

This is the structure of the tables involved:

phpcoin_clients:
Field Type Null Default
cl_id int(11) No 0
cl_join_ts varchar(10) Yes NULL
cl_status varchar(20) No pending
cl_company varchar(50) No
cl_name_first varchar(20) No
cl_name_last varchar(20) No
cl_addr_01 varchar(50) No
cl_addr_02 varchar(50) No
cl_city varchar(50) No
cl_state_prov varchar(50) No
cl_country varchar(50) No
cl_zip_code varchar(12) No
cl_phone varchar(20) No
cl_email varchar(50) No
cl_user_name varchar(20) No
cl_user_pword varchar(50) No
cl_notes text No
cl_groups int(11) No 0

phpcoin_clients_contacts:
Field Type Null Default
contacts_id int(11) No
contacts_cl_id int(11) No 0
contacts_name_first varchar(20) No
contacts_name_last varchar(20) No
contacts_email varchar(50) No

The sql query is as follows:

SELECT cl_id from phpcoin_clients, phpcoin_clients_contacts
WHERE (cl*************@domain.com')
OR (co*******************@domain.com')
AND contacts_cl_id=cl_id

This only returns the client id (cl_id) when the contacts_email is set
to em***@domain.com.

If the query is just:
SELECT cl_id from phpcoin_clients
WHERE (cl*************@domain.com')

Then the result is as expected, anyone have any ideas as to why this is?

Thanks,
Ben
Aug 30 '05 #1
3 1301
Ben Allen wrote:
SELECT cl_id from phpcoin_clients, phpcoin_clients_contacts
WHERE (cl*************@domain.com')
OR (co*******************@domain.com')
AND contacts_cl_id=cl_id

This only returns the client id (cl_id) when the contacts_email is set
to em***@domain.com.


Yes, this is working correctly, because AND binds tighter than OR.

(X OR Y AND Z) is the same as (X OR (Y AND Z)).
You need to use parentheses to override that behavior and get
((X OR Y) AND Z).

So you could rewrite the query as follows:

SELECT cl_id from phpcoin_clients, phpcoin_clients_contacts
WHERE (cl*************@domain.com' OR co*******************@domain.com')
AND contacts_cl_id=cl_id

Regards,
Bill K.
Aug 30 '05 #2
Bill Karwin wrote:
Ben Allen wrote:
SELECT cl_id from phpcoin_clients, phpcoin_clients_contacts
WHERE (cl*************@domain.com')
OR (co*******************@domain.com')
AND contacts_cl_id=cl_id

This only returns the client id (cl_id) when the contacts_email is set
to em***@domain.com.

Yes, this is working correctly, because AND binds tighter than OR.

(X OR Y AND Z) is the same as (X OR (Y AND Z)).
You need to use parentheses to override that behavior and get
((X OR Y) AND Z).

So you could rewrite the query as follows:

SELECT cl_id from phpcoin_clients, phpcoin_clients_contacts
WHERE (cl*************@domain.com' OR co*******************@domain.com')
AND contacts_cl_id=cl_id

Regards,
Bill K.

Thanks that's working better, however, now if there is no cl_id in the
contacts table (contacts_cl_id), then nothing is returned. is this
because the line

SELECT cl_id from phpcoin_clients, phpcoin_clients_contacts

is being called even though there is no cl_id in
phpcoin_clients_contacts just contacts_cl_id?

Thanks Again,
Ben
Aug 30 '05 #3
Ben Allen wrote:
now if there is no cl_id in the
contacts table (contacts_cl_id), then nothing is returned.


This is because your query is of the type that is termed an "inner
join". Records are part of the query's result set only when
contacts_cl_id = cl_id. This means that there _must_ be a matching
value in both tables for that condition to be satisfied.

You probably need an "outer join." This is the type of join that
returns all records from one table, even if no matching records exist in
the other table. If you were selecting fields from the second table in
the select-list, they'd show up as NULL if there were no record matching
the first table.

SELECT cl.cl_id
FROM phpcoin_clients AS cl LEFT OUTER JOIN
phpcoin_clients_contacts AS co
ON cl.cl_id = co.contacts_cl_id
WHERE cl****************@domain.com'
OR co**********************@domain.com'

Regards,
Bill K.
Aug 31 '05 #4

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

Similar topics

2
by: Yulia Yegenov | last post by:
I have a query that looks like this: (I insert the date created with the php date function) $status = "Active"; //(I cannot use the mysql timestamp function for other reason). $curdate =...
1
by: Saqib Ali | last post by:
I have created 2 tables in my MySQL database. A_TAB and B_TAB. They have auto-incrementing integer primary keys respectively named A_ID & B_ID. When I created B_TAB, I declared a field named A_ID...
1
by: Good Man | last post by:
Hi there I've noticed some very weird things happening with my current MySQL setup on my XP Laptop, a development machine. For a while, I have been trying to get the MySQL cache to work....
3
by: Auddog | last post by:
I have the following query that works in mysql: select id, order_no, price, count(item_no), sum(price) from production WHERE item_no = '27714' group by item_no; When I setup my query in php,...
12
by: Martien van Wanrooij | last post by:
I have been using for a while a class to create photo albums. Shortly, an array of photo's is declared, initially they are shown as thumbnails, by clicking on a thumbnail a larger photo is shown...
3
by: Taras_96 | last post by:
Hi everyone, I'm having a bit of trouble understanding the purpose of escaping nulls, and the use of addcslashes. Firstly, the manual states that: "Strictly speaking, MySQL requires only...
7
by: eholz1 | last post by:
Hello PHP group, Could someone help me out? I have PHP 5.2, Apache 2.0, and MySQL 5.0 running on Linux (Redhat Fedora Core 6). All that works fine. I would like to be able to "call" a stored...
8
by: The Natural Philosopher | last post by:
This is so weird. What I am trying to do is to upload files and stuff them in a mysql database. Everything works except the file content is zero. using the load_file command from mysql...
221
Atli
by: Atli | last post by:
You may be wondering why you would want to put your files “into” the database, rather than just onto the file-system. Well, most of the time, you wouldn’t. In situations where your PHP application...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
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,...

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.