Small Problem with connecting with SQL

Discussion in 'PHP' started by AAZZ, May 15, 2010.

  1. #1
    Hello,

    I was just wondering if I can get some simple code help. I looked pretty much everywhere and wasn't able to resolve my problem.

    I am simply trying to connect to my database (new one) through code. First I go to my cPanel (cPanel 11), create the Database and username I want, connect both of them with all privileges. Then I use a simple MySQL code to connect to it.

    <?php
    $db = mysql_connect("localhost","db_name","db_pass");
    if (!$db){
    die('Could not connect: ' . mysql_error());
    }else{
    echo "hello\n";
    }
    if (mysql_query("CREATE DATABASE Persons",$db)){
    echo "Database created";
    }else{
    echo "Error creating database: " . mysql_error();
    }
    mysql_close($db);
    ?>
    Code (markup):
    For some reason It gives me an error that I don't have permission. What am I missing? I did this a few months ago and I know I am making some dumb mistake. Please help.

    -Thanks in advance
     
    AAZZ, May 15, 2010 IP
  2. gapz101

    gapz101 Well-Known Member

    Messages:
    524
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    150
    #2
    if (mysql_query("CREATE DATABASE Persons",$db)){ <--- here: why do you need to create another db? with cpanel, you can only add database on cpanel, unless you have root or special case set.
     
    gapz101, May 15, 2010 IP
  3. rainborick

    rainborick Well-Known Member

    Messages:
    424
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    120
    #3
    To be specific, your host does not allow the usernames that you create for mySQL access to use the CREATE function to make new databases. Only the username invoked by the cPanel software has this permission. This is a very common practice for hosting services. It just means that your scripts can't create new databases. You have to do that beforehand in cPanel.
     
    rainborick, May 15, 2010 IP
  4. AAZZ

    AAZZ Active Member

    Messages:
    1,612
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    90
    #4
    Hello,

    I was reading MySQL tutorials and that is how they showed. I understand the cPanel allows me to make a database and username. Is it safe to assume that now I simply connect to THTA database and start making my tables, etc...?

    -Thanks
     
    AAZZ, May 15, 2010 IP
  5. gapz101

    gapz101 Well-Known Member

    Messages:
    524
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    150
    #5
    yup, creating tables is Okay if you have set it (i mean permissions)
     
    gapz101, May 15, 2010 IP