In this function create I am trying to create a database. However, it fails to create and displays the error "Error creating database: Access denied." SO this means there is a connection and makes it passed the first check. Then when the mysql_query is called, it doesnt return true. Can anyone help?

function create($dbName, $tbName, $fields, $types_sizes, $PK)
        {
            $sql = null;
            $con = mysql_connect("borg.cs.up.ac.za","username", "password");

            if (!$con)
            {
                die('Could not connect: ' . mysql_error());
            }
            else 
                {
                  if(mysql_query("CREATE DATABASE". $dbName, $con))
                    {
                        echo "Database created";
                        $dbExists = 1;
                    }
                   else
                    {
                        echo "Error creating database: " . mysql_error();
                        $dbExists = 0;
                    }
                   $i = 0;
                    if(($con)&&($bdExists = 1))
                    {
                        $sql . "CREATE TABLE " . $tbName . "(";
                        while($i < count($fields))
                        {
                            $sql . $fields[$i] . " " + $types_sizes[$i] . ",";
                            $i++;
                        }
                    if($PK != null)
                    {
                        $sql . "PRIMARY KEY (" . $PK .")";
                    }
                    }
                mysql_query($sql,$con);
                mysql_close($con);
            }
         }
share|improve this question
2  
You probably do not have the necessary privileges to create databases on the account you connect to the server. – Jeroen Bolle May 14 '12 at 13:11
Also you may set prefix ($dbName_.'tablename') in name for separate tables – Sergey May 14 '12 at 13:12
Create the database using PhpMyAdmin or MySQL Workbench then just create the tables via code. It seems you do not have the required permissions to create a database. – Omtara May 14 '12 at 13:13
I do have the full amount of privileges so thats not a problem.I have an idea its got something to do with the connection, and not the query. – Chris May 14 '12 at 13:16
@Chris the error message exactly telling you that you don't have the privileges. You wouldn't have got that message had it been connection issue. and mysql_* is deprecated officailly. You should learn mysqli or better PDO. – itachi May 14 '12 at 13:17

2 Answers

you have not given space in the line

if(mysql_query("CREATE DATABASE". $dbName, $con)), please provide space after "Create Database" -> "create database ".$dbname, because otherwise it will be database+name combined.

share|improve this answer
this query didn't run in the 1st place because access was denied. – itachi May 14 '12 at 13:21
let the user run it once and then decide whether it is working or not. – Gaurav Bansal May 14 '12 at 13:22
OK so @Gaurav Bansal. Thanks. There was a space missing. But now its giving me this error "Error creating database: Access denied for user 's11028786'@'%' to database 'books' ", any ideas? i have to have access, because i successfully connect in my previous function. – Chris May 14 '12 at 13:26
2  
If you get an error such as Access denied for user 'monty'@'localhost' to database 'menagerie' when attempting to create a database, this means that your user account does not have the necessary privileges to do so. ----- mysql dev guide. – itachi May 14 '12 at 13:27

Try this. This is untested.Changed some lines in your code.

function create($dbName, $tbName, $fields, $types_sizes, $PK)
    {
        $sql = null;
        $con = mysql_connect("borg.cs.up.ac.za","username", "password");

        if (!$con)
        {
            die('Could not connect: ' . mysql_error());
        }
        else 
            {
    if(mysql_select_db($dbName))
                {
                    echo "Database selected";
                    $dbExists = 1;
                }
               else
                {
                    echo "Error creating database: " . mysql_error();
                    $dbExists = 0;
                }
               $i = 0;
                if(($con)&&($dbExists == 1))
                {
                    $sql = "CREATE TABLE " . $tbName . "(";
                    while($i < count($fields))
                    {
                        $sql .= $fields[$i] . " " + $types_sizes[$i] . ",";
                        $i++;
                    }
                if($PK != null)
                {
                    $sql .= "PRIMARY KEY (" . $PK .")";
                }
                }
            mysql_query($sql,$con);
            mysql_close($con);
        }
     }
share|improve this answer

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.