douboshan1466 2014-02-02 08:54
浏览 63
已采纳

pg_query_params插入默认值或使用函数时出错

pg_query_params seems not working as expected:

this works:

INSERT INTO users (username,password,secretcode) VALUES 'test',crypt(:password,gen_salt(md5)), 'DEFAULT'

secretcode field have a default generated value

This doesn't works:

pg_query_params($Db, 'INSERT INTO users (username,password,secretcode) VALUES ($1, $2, $3)', array ($USR,crypt($PWD,gen_salt(md5)),'DEFAULT');

password field contains the string 'crypt($PWD,gen_salt(md5))'

secretcode field contains 'DEFAULT' string

Is this the correct behavior or a bug ?

Thanks

Dan Perez

  • 写回答

1条回答 默认 最新

  • dongronge3732 2014-02-03 04:28
    关注

    Parameters are designed to send exactly the literal text you wrote. The whole point is that they don't execute functions or otherwise get interpreted as SQL syntax.

    DEFAULT is a keyword. That's not what you're supplying, you're sending the text 'DEFAULT'.

    Similarly, you're sending the text crypt($PWD,gen_salt(md5)) to PostgreSQL.

    If you want to insert using a query you can do that, but you must modify the SQL. E.g.

    pg_query_params(
      $Db,
      'INSERT INTO users (username,password,secretcode) VALUES ($1, crypt($2, gen_salt('md5')), DEFAULT)', array ($USR,$PWD)
    );
    

    As you can see, you can put arbitrary expressions in the VALUES clause, as well as keywords like DEFAULT or NEW. You can't pass them as query parameters.

    I'd really like it if there were "special" client values that you could pass, magic constants that were interpreted as keywords like DEFAULT. It doesn't work that way, though; the DEFAULT keyword is interpreted at parse/plan time, not parameter binding time.

    Also, you must quote the hash type in gen_salt - 'md5' not just md5.

    Finally - crypt is not an acceptable way to hash a password.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?