2

I try to make the following sql query from phpMyAdmin who works perfectly and return 1 result with doctrine 1 but i get an exception :

SQLSTATE[42S22]: Column not found: 1054 Champ 'MOY1100' inconnu dans where clause. Failing Query: "select id_au FROM acteur_unite WHERE code_unite = MOY1100 LIMIT 1"

Here the sql query who work on phpMyAdmin :

SELECT id_au FROM acteur_unite WHERE code_unite = 'MOY1100' LIMIT 1

Here my query with doctrine :

 public function getId($code_unite) {

        $con = Doctrine_Manager::getInstance()->connection();

        $st = $con->execute("select id_au FROM acteur_unite
            WHERE code_unite = $code_unite LIMIT 1");

        $id = null;
        // fetch query result
        $data = $st->fetch(PDO::FETCH_ASSOC);
        $id = $data['id_au'];
        return $id;
    }

Where i'm wrong ?

Thanks a lot in advance

goto
  • 7,908
  • 10
  • 48
  • 58
Mathieu Mourareau
  • 1,140
  • 2
  • 23
  • 47
  • Possible duplicate of [Using Raw SQL with Doctrine](https://stackoverflow.com/questions/2774947/using-raw-sql-with-doctrine) – goto Feb 01 '18 at 08:49

1 Answers1

3

seems you missing the quote around var $code_unite

  $st = $con->execute("select id_au FROM acteur_unite
        WHERE code_unite = '$code_unite' LIMIT 1");

but be careful with the use of var in sql .. you are at risk for sql injection . Then check for your framework the right way for the param_binding .. for avoid this risk

eg:

  $st = $con->execute("select id_au FROM acteur_unite
    WHERE code_unite = :code_unite LIMIT 1");

  $st->bindParam(':code_unite', $code_unite, PDO::PARAM_STR);
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107