Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

madsynn's avatar

Need Help with DB Query for geo search.

I have a client that is on a shared hosting plan and i have a problem. He cannot upgrade to a plan that will allow us to set mysql globals. My problem is i need to be able to run this I cannot.

Here is what i need to run and cannot.

SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));

Here is the query we need. Can anyone help me with a alternative regarding this.

Here are the details.

        $locations = DB::table('locations')
            ->join('contractor_location', 'locations.id', '=', 'contractor_location.location_id')
            ->join('contractors', 'contractor_location.contractor_id', '=', 'contractors.id')
            ->select('locations.city','locations.latitude','locations.longitude')
            ->where('locations.published',1)
            ->where('contractors.published',1)
            //->groupBy('locations.city')
            ->orderBy('locations.title','ASC')
            ->get();

We have the location city turned off because it wont work unless we can run that global setting.

Do any of you know a way around this we really need to get around this problem. We also need to add radius to the query and any guidance would be appreciated.

0 likes
5 replies
kima's avatar

please try this:

        $locationsQuery = DB::table('locations')
                       ->join('contractor_location', 'locations.id', '=', 'contractor_location.location_id')
                       ->join('contractors', 'contractor_location.contractor_id', '=', 'contractors.id')
                       ->select('locations.city','locations.latitude','locations.longitude')
                       ->where('locations.published',1)
                       ->where('contractors.published',1)
            //->groupBy('locations.city')
                       ->orderBy('locations.title','ASC');
        
        $locations = DB::select("SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY','')); {$locationsQuery->toSql()}", $locationsQuery->getBindings());
madsynn's avatar

@kima

Thanks, but now i am getting this error.

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'select `locations`.`city`, `locations`.`latitude`, `locations`.`longitude` fr...' at line 1 (SQL: SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY','')); select `locations`.`city`, `locations`.`latitude`, `locations`.`longitude` from `locations` inner join `contractor_location` on `locations`.`id` = `contractor_location`.`location_id` inner join `contractors` on `contractor_location`.`contractor_id` = `contractors`.`id` where `locations`.`published` = 1 and `contractors`.`published` = 1 order by `locations`.`title` asc)
kima's avatar

do you receive the same error when you execute the errored SQL directly in your DB server?

Please or to participate in this conversation.