Unable to create database through PHP.

Discussion in 'PHP' started by saadi123, Jan 30, 2014.

  1. #1
    I'm trying to create a database in my wamp server but every time the following error appears:

    "Error Creating database Access denied for user ''@'localhost' to database 'my_database'"

    I've checked from the cmd prompt and my user is root@localhost. I tried to create the same database from phpmyadmin and it was created.

    Can anyone tell me what the real problem is?
     
    saadi123, Jan 30, 2014 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Since you're not giving us any codes, it's a little hard to say, but it should be pretty obvious from the error-message, shouldn't it? The user ''@'localhost' is NOT the same as root@localhost, now, is it?
     
    PoPSiCLe, Jan 30, 2014 IP
  3. saadi123

    saadi123 Well-Known Member

    Messages:
    196
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #3
    Here's my code:
    <?php

    $con = mysqli_connect();
    if (mysqli_connect_errno()) {
    echo "Failed to connect " . mysqli_connect_error();
    }
    else {
    echo "Connected <br>";
    }
    $sql = "CREATE DATABASE my_database";
    if (mysqli_query($con, $sql)) {
    echo "Database created";
    }
    else {
    echo "Error Creating database " . mysqli_error($con);
    }

    ?>

    Well the database is connecting pretty well with the above code. So seems like that the there's no problem with the connection. I've also checked the privileges in the phpmyadmin and I've got complete privileges as well.

    I will really appreciate any help regarding this.
     
    saadi123, Jan 30, 2014 IP
  4. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #4
    stupid question does my_database already exist?
     
    stephan2307, Jan 30, 2014 IP
  5. saadi123

    saadi123 Well-Known Member

    Messages:
    196
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #5
    It wasn't there before. But now it does exist. I created it directly from phpmyadmin.
    However, the error before creating the database through phpmyadmin was same as mentioned before.
     
    saadi123, Jan 31, 2014 IP
  6. saadi123

    saadi123 Well-Known Member

    Messages:
    196
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #6
    Ok, so I also checked my phpadmin panel. The username it shows is 'root' and the host is 'localhost'. So I changed my PHP query (mentioned above) to these two codes:
    (mysqli_connect_errno('localhost', 'root', ''))
    (mysqli_connect_errno('localhost', 'root'))

    But still no use. :-(

    @PoPSiCLe : Your suggestion seems to be making sense as the following message suggests that the server is not able to find the required user:
    "Error Creating database Access denied for user ''@'localhost' to database 'my_database"

    So the question is how can I access the user?
     
    saadi123, Jan 31, 2014 IP
  7. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #7
    you need to provide them in the first function, the $con-variable.
    doing this from memory, now, but it's something like this:
    $con = mysqli_connect('mysql:host=localhost','user=root');
     
    PoPSiCLe, Jan 31, 2014 IP
  8. saadi123

    saadi123 Well-Known Member

    Messages:
    196
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #8
    Way to go @PoPSiCLe . That finally worked!
    Man! that's too dumb of me.
     
    saadi123, Jan 31, 2014 IP