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 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据