Author Topic: Where is this query going wrong? Something to do with the WHERE part....  (Read 323 times)

0 Members and 1 Guest are viewing this topic.

Offline richrockTopic starter

  • Enthusiast
  • Posts: 206
  • Gender: Male
  • Web Developer
    • View Profile
    • blabr - mouthing off nonsense
Hi,

Got a wierd prob with a query string, and here it is:

Code: [Select]
SELECT bi.id, bi.author_id, bi.firstname, bi.secondname, bi.thirdname, bi.fourthname, bi.fifthname, bi.surname, bi.dates, bi.biography, bi.created, au.name AS author, au.id AS auth_id
FROM jos_yearbook_biographies bi
LEFT JOIN jos_yearbook_authors au ON bi.author_id = au.id
WHERE bi.author_id = '1'
AND bi.surname LIKE 'cuy%'
OR bi.firstname LIKE 'cuy%'
OR bi.secondname LIKE 'cuy%'
OR bi.thirdname LIKE 'cuy%'
OR bi.fourthname LIKE 'cuy%'
OR bi.fifthname LIKE 'cuy%'
OR bi.biography LIKE '%cuy%'

The problem is with the author_id - it seems to ignore it completely, and shows a result regardless.  In the DB I've got two items, one with author_id 1, and the other with 2.

It shows the item with an author_id of two every time - I've checked the code, and it's sending 1 as seen in the statement.

I've also tried it using this statement:

Code: [Select]
SELECT bi.*, au.name AS author, au.id AS auth_id FROM #__yearbook_biographies bi
LEFT JOIN #__yearbook_authors au ON bi.author_id = au.id WHERE bi.surname LIKE 'cuy%'
OR bi.firstname LIKE 'cuy%'
OR bi.secondname LIKE 'cuy%'
OR bi.thirdname LIKE 'cuy%'
OR bi.fourthname LIKE 'cuy%'
OR bi.fifthname LIKE 'cuy%'
OR bi.biography LIKE '%cuy%' AND bi.author_id = '1'

(I know one has jos_ and the other has #__ - I was testing the variant in phpmyadmin)

Any ideas why I always get items with author_id of '2'???  Puzzled.
If at first you don't succeed, you fail.

Offline Mchl

  • Staff Alumni
  • Freak!
  • *
  • Posts: 8,582
  • Gender: Male
  • That's Largo in my avatar, not me.
    • View Profile
    • FlingBits
Re: Where is this query going wrong? Something to do with the WHERE part....
« Reply #1 on: February 08, 2010, 12:13:15 PM »
Code: [Select]
WHERE bi.author_id = '1' AND (bi.surname LIKE 'cuy%' OR ... OR... OR ...)
NetBeans fanatic | ExtJS masochist | C++ denier
PHP4 & MySQL4 are no longer supported.
PHPFreaks Tutorials | PHP Debugging: A Beginner's guide | PHP Security Tutorial || How To Ask Questions The Smart Way
Flingbits tutorials | Class Autoloading

Offline richrockTopic starter

  • Enthusiast
  • Posts: 206
  • Gender: Male
  • Web Developer
    • View Profile
    • blabr - mouthing off nonsense
Re: Where is this query going wrong? Something to do with the WHERE part....
« Reply #2 on: February 09, 2010, 06:03:15 AM »
Excellent - did the trick!

Was part way there with putting the author id first.  Thanks  :D
If at first you don't succeed, you fail.

Offline Mchl

  • Staff Alumni
  • Freak!
  • *
  • Posts: 8,582
  • Gender: Male
  • That's Largo in my avatar, not me.
    • View Profile
    • FlingBits
Re: Where is this query going wrong? Something to do with the WHERE part....
« Reply #3 on: February 09, 2010, 07:40:30 AM »
Remember, AND is like multiplicatin, OR like addition. If you want to multiply 5 by sum of 2 and 3 you need to write
5 * (2+3) (=25)
 not
5*2+3 (=13)
NetBeans fanatic | ExtJS masochist | C++ denier
PHP4 & MySQL4 are no longer supported.
PHPFreaks Tutorials | PHP Debugging: A Beginner's guide | PHP Security Tutorial || How To Ask Questions The Smart Way
Flingbits tutorials | Class Autoloading