1
votes
        <?php
            $name= $_POST["dbname"];
            $pass= $_POST["dbpass"];
            $rname= $_POST["rootname"];
            $rpass=$_POST["rootpass"];

            if ($rname='leave blank for default root name' or $name='')
                {
                    $rname="root";
                }
            if ($rpass='leave blank if no password assigned')
                {
                    $rpass='';
                }

            $con = mysql_connect("localhost", $rname, $rpass);

            if (!$con)
                {

                  die('Could not connect: ' . mysql_error());
                }

                    if (mysql_query("CREATE DATABASE {$name}",$con))
                        {
                          echo "Database created";
                         }
                        else
                          {
                              echo "Error creating database: " . mysql_error();
                          }
                    (mysql_query("DATABASEPASSWORD {$pass}",$con));


                mysql_close($con);      
        ?>

this is the php script i am using to create database,i am passing rootname=root and rootpass=toor

but i am getting an error

Warning: mysql_connect(): Access denied for user 'root'@'localhost' (using password: NO) in /var/www/webdefender/script/dbcreate.php on line 17 Could not connect: Access denied for user 'root'@'localhost' (using password: NO)

but when i use

$con = mysql_connect("localhost", 'root','toor' );

it works fine

Please help

3
If password is set, you must have to specify password when you connect with serverButani Vijay

3 Answers

4
votes

You're assigning values in your first 2 checks instead of checking them for equality.

$a = 1; // assigning
$a == 1; // this is a check if 1 is $a is equal to 1

So you're basically resetting your $rpass value to '' (empty string) in:

if ($rpass='leave blank if no password assigned')
{
   $rpass='';
}

Change your checks to use == and it will work.

Please to be aware that using root access from scripts is a HUGE security hazard.

0
votes

Don't use or use below code near if ($rname='')

if ($rname='')
{
    $rname="root";
}
0
votes

it's general when U use if like this

if($a=b)

always return true and $a have the value b
the right Syntax is

if($a==b)

so if and only if $a value = b it return true