1

I am a beginner in psql and php. I do want to write a php script for creating a database and user and grant all privileges of database to the newly created user.I already write a php script for creating the mysql database and user as follows. I do want to convert this script as to useful for 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>');
         }
?>
nidhin
  • 359
  • 1
  • 3
  • 13
  • 1
    ***Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).*** [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Oct 24 '16 at 15:39
  • [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)***. Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Oct 24 '16 at 15:39
  • 1
    According to the tag, PSQL is an interactive terminal program for using Postgres databases. It's not something you would do from PHP. Did you mean PostgreSQL? Anyway, SO is not a place to get people to write code for you. Read the Postgres documentation and try to do it yourself. If you can't get it working, post your attempt here and we'll help. – Barmar Oct 24 '16 at 15:53

0 Answers0