...

View Full Version : Selecting from 3 tables inc. search



rdl
05-19-2011, 01:58 AM
Hi,
I have the following query...

select distinct * from report, reportofficer, reportperson
where report.reportref = 17
and reportofficer.reportref = 17
and reportperson.reportref = 17
group by report.reportref;



this works in phpmyadmin console...however when trying it in .php file it isn't working.

In the php file I have created a form where a user can enter the reportref, town, category or reportstatus ;(user can choose any of them) and on clicking search button, I want some fields of these tables to be shown please.

I have tried the below query in php file which isn't working...

[code]
$result = mysql ("select report.ReportRef, report.ReportDate,report.Category, report.Street, report.Town, report.Status, person.ID,
person.Surname, person.Name, officer.OfficerID
from report, reportofficer, reportperson where
report.ReportRef = reportofficer.ReportRef AND reportofficer.ReportRef = reportperson.ReportRef AND report.reportref = LIKE '%" . $search . "%'
or report.ReportRef = reportofficer.ReportRef AND reportofficer.ReportRef = reportperson.ReportRef AND Town LIKE '%" . $Town . "%'
or report.ReportRef = reportofficer.ReportRef AND reportofficer.ReportRef = reportperson.ReportRef AND Category LIKE '%" . $Category . "%'
or report.ReportRef = reportofficer.ReportRef AND reportofficer.ReportRef = reportperson.ReportRef AND ReportStatus LIKE '%" . $ReportStatus . "%' ");




However when trying this - using only one table in .php file, it worked and gave good results..

[code]
$result = mysql_query("SELECT * FROM report WHERE ReportRef LIKE '%" . $search . "%' or Town LIKE '%" . $Town . "%' or Category LIKE '%" . $Category . "%' or ReportStatus LIKE '%" . $ReportStatus . "%' ");


Thankyou,
rdl

shadowmaniac
05-19-2011, 02:34 AM
I ought to brush up on my SQL skills.
Not sure if it's a typo or not but you have an extra = sign right before the first LIKE in your SQL statement.


report.ReportRef = reportofficer.ReportRef AND reportofficer.ReportRef = reportperson.ReportRef AND report.reportref = LIKE '%" . $search . "%'



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum