How to get SQL after prepare

Source: Internet
Author: User
$db = new PDO();$sth = $db->prepare('sql');// 有木有一个类似getSql的方法可以获取执行的SQL的= =

And then

$db = new PDO();$sth = $db->prepare('select * from `table` where p1 = :p1 limit :limit');$sth->bindValue(':p1',$var_p1,PDO::PARAM_INT);$sth->bindValue(':limit',$var_limit,PDO::PARAM_INT);# ^ 这样正确$sth->execute(array(intval($var_p1),intval($var_limit));# ^ 为毛报错?,execute中,转成int还是不行?非要我指定一个绑定类型?

Reply content:

$db = new PDO();$sth = $db->prepare('sql');// 有木有一个类似getSql的方法可以获取执行的SQL的= =

And then

$db = new PDO();$sth = $db->prepare('select * from `table` where p1 = :p1 limit :limit');$sth->bindValue(':p1',$var_p1,PDO::PARAM_INT);$sth->bindValue(':limit',$var_limit,PDO::PARAM_INT);# ^ 这样正确$sth->execute(array(intval($var_p1),intval($var_limit));# ^ 为毛报错?,execute中,转成int还是不行?非要我指定一个绑定类型?

Not a type of problem, take a closer look at the documentation: http://cn2.php.net/manual/zh/pdostatement.execute.php

You do this by using the question-mark placeholder when you use the Execute call method, and if you use the "Colon parameter name", the array passed at Execute will also use the colon-argument-prepare to set the subscript.

Also, do you want to get the SQL statement after replacing the placeholder with the actual parameter value? PDO is impossible because prepared statement is essentially a temporary stored procedure

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.