简体   繁体   中英

Query with doctrine 1

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

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);

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