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 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题