我是psql和php的初学者。我想写一个用于创建数据库和用户的php脚本,并将数据库的所有权限授予新创建的用户。我已经编写了一个用于创建mysql数据库和用户的php脚本,如下所示。我确实希望将此脚本转换为对psql有用。
<?php
$host = 'localhost';
$db = $db_name;
$user = $user;
$passwd = $passwd;
#connectong to the host
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
#die('Could not connect: ' . mysql_error());
echo '<font color="red">could not connect to database</font>';
}
#creating user
$sql1 = "CREATE USER '{$user}'@'{$host}' IDENTIFIED BY '{$passwd}'";
$ret = mysql_query( $sql1, $conn );
if(! $ret ) {
die('<font color="red">could not create database user username already exists</font>');
#echo '<font color="red">could not create user username already exists</font>';
}
#creating database
$sql2 = 'CREATE DATABASE '.$db;
$retval = mysql_query( $sql2, $conn );
if(! $retval ) {
#die('Could not create database: ' . mysql_error());
#echo '<font color="red">could not create database already exists</font>';
die('<font color="red">could not create database, database already exists</font>');
}
$sql3 = "GRANT ALL ON ".$db.".* TO '".$user."'@'localhost';";
$test = mysql_query( $sql3, $conn );
if(!$test ) {
#die('Could not giving permission: ' . mysql_error());
#echo '<font color="red">could not giving permission</font>';
die('<font color="red">could not giving permission</font>');
}
?>