【问题标题】:How to create a postgres user with privileges using php query如何使用 php 查询创建具有权限的 postgres 用户
【发布时间】:2020-01-04 22:05:51
【问题描述】:

我想使用 php 在我的数据库中的一个表上创建一个具有所有权限的用户,但我的代码无法正常工作。 我试过这段代码:

 $query="CREATE USER $1 WITH password $2";
 $result = pg_prepare($dbh, "", $query);
 $result = pg_execute($dbh, "", array($utente, md5($psw)));

 $query="GRANT ALL PRIVILEGES ON match to $1";
 $result = pg_prepare($dbh, "", $query);
 $result = pg_execute($dbh, "", array($utente));

然后我尝试将其更改为将变量移到字符串之外,但它也不起作用。

 $query="CREATE USER " .$utente . " WITH password ". md5($psw);
 $result = pg_prepare($dbh, "", $query);
 $result = pg_execute($dbh, "", array());

 $query="GRANT ALL PRIVILEGES ON match to " . $utente;
 $result = pg_prepare($dbh, "", $query);
 $result = pg_execute($dbh, "", array());

$result 是假的,在我的数据库中我没有得到我想要创建的新用户。

【问题讨论】:

  • 能否详细说明我的代码无法正常工作 的含义?你有错误吗?
  • @GMB 我的意思是 pg_execute 返回 false,它什么也没做。我的数据库中没有新用户

标签: php sql postgresql privileges


【解决方案1】:
  • 用户名是一个标识符,可以用双引号括起来 "" 或保持原样。
  • 密码是文本值,必须用单引号括起来''

一般来说:传递给查询的所有值都应被视为准备好的语句变量。在这种情况下,我无法用pg_prepare 中的参数数组中的值替换$1

所以这是一个通过“手”转义的工作示例。

$utente = pg_escape_identifier('username');
$psw = pg_escape_string('pass');

$query="CREATE USER {$utente} WITH password '{$psw}'";
pg_prepare($dbh, "", $query);
$result = pg_execute($dbh, "", []);

【讨论】:

    猜你喜欢
    • 2019-11-08
    • 2016-09-16
    • 2021-05-23
    • 1970-01-01
    • 2011-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多