简体   繁体   中英

Syntax error 1064 in SQL Query in selecting an id from database : Symfony

I am having this error in my SQL query when i try to view profile of any organiser:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

This is my controller function:

public function viewProfileAction(Request $request) {
    $em = $this->getDoctrine()->getManager();

    $orgid = $this->getUser()->getId();

    $clicknblog = $this->get('adventure_bridge.clicknblog');
    $trails= $clicknblog->matchtrails($orgid);



    $clicknblog = $this->get('adventure_bridge.clicknblog');
    $event= $clicknblog-> getEvents($orgid);

    $user = $this->getUser();
    $memberId = $user->getId();
    $role = 'BidOrganiser';

    $organiserDetails = $em->getRepository('AdventureBiddingBundle:BidOrganiser')->getOrganiserByMemberId($memberId,$role);

    foreach($organiserDetails as $organiser){

        $count= $organiser->getCountry();
        $country = $clicknblog->getCountriesName($count);

    }
      //$countries = $clicknblog->getCountries('');

      $organiser->setCountry($country);
    //redirecting to the user profile in view mode
    return $this->render('AdventureBiddingBundle:Organiser:viewProfileOrganiser.html.twig',array(
            'member' => $organiser,
            'trails' => $trails,
            'event'=> $event

    ));
}

And this is the service class function where the error is:

 public function getCountriesName($id) {

   $stmt = $this->connection->query("SELECT name FROM countries where id = $id"); // This line is having an error
       $cid3 = $stmt->fetchAll(PDO::FETCH_ASSOC);

       foreach($cid3 as $value){
          foreach($value as $country){
          }
       }
  return $country;

}

What is the problem in the sql syntax, if anyone can resolve Thank you

Change the query to -

"SELECT `name` FROM countries where id = '$id'"

You are using $country outside the loop. It is not accessible there. Should be -

   foreach($cid3 as $value){
      foreach($value as $country){
         return $country; // Something like this
      }
   }

return would not let the script to execute the whole loop. If you want then store it in a variable and then return.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM