0

I'm trying to get MariaDb to

a) create a database

b) create a user for the database

c) create a table for the database

d) insert data into the new table

However, the code doesn't

a) create a table

b) log in as the new user and insert the data into the table

The PHP code:

$MariaDb_User_Username = "username_a";
$MariaDb_User_Password = "password_a";
$MariaDb_User_Database = "database_a";

$Admin_Username = "Admin";
$Admin_Password = "asd123";

try{
    $Dbh1 = new PDO("mysql:host=127.0.0.1:3307", $Admin_Username, $Admin_Password);
    $Dbh1->exec("CREATE DATABASE `{$MariaDb_User_Database}`;");         //'
    echo "A complete<br>";
}
catch (PDOException $e) 
{
    echo 'A Error:'.$e->getCode().':<br>'.$e->getMessage().'<br>'; //die($e->getMessage()); //'.$e->errorInfo().'<br>
}

try{
    $Dbh2 = new PDO("mysql:host=127.0.0.1:3307", $Admin_Username, $Admin_Password);
    $Dbh2->exec("CREATE USER IF NOT EXISTS `{$MariaDb_User_Username}`@`127.0.0.1:3307` IDENTIFIED BY `{$MariaDb_User_Password}`;
            GRANT SELECT, INSERT, UPDATE, DELETE ON `{$MariaDb_User_Database}`.* TO `{$MariaDb_User_Username}`@`127.0.0.1:3307`; 
            FLUSH PRIVILEGES;");
    echo "B complete<br>";
}
catch (PDOException $e) 
{
    echo 'B Error:'.$e->getCode().':<br>'.$e->getMessage().'<br>'; //die($e->getMessage()); //'.$e->errorInfo().'<br>
}

try{
    $Dbh3 = new PDO("mysql:host=127.0.0.1:3307", $Admin_Username, $Admin_Password);
    $Dbh3->exec("CREATE TABLE Tablename(
     rowid INT( 255 ) AUTO_INCREMENT PRIMARY KEY,
     fname VARCHAR( 255 ) NOT NULL, 
     sname VARCHAR( 255 ) NOT NULL,
     age INT( 3 )");
     echo "C complete<br>";
}
catch (PDOException $e) 
{
    echo 'C Error:'.$e->getCode().':<br>'.$e->getMessage().'<br>'; //die($e->getMessage()); //'.$e->errorInfo().'<br>
}

try{
    $Dbh4 = new PDO("mysql:host=127.0.0.1:3307;dbname=$MariaDb_User_Database", $MariaDb_User_Username, $MariaDb_User_Password); 
    $statement = $Dbh4->prepare('INSERT INTO '.$MariaDb_User_Database.' (name, lastname, age)
        VALUES (:fname, :sname, :age)');

    $statement->execute([
        'fname' => 'Bob',
        'sname' => 'Desaunois',
        'age' => '18',
]);     
    echo "D complete<br>";
}
catch (PDOException $e) 
{
    echo 'D Error:'.$e->getCode().':<br>'.$e->getMessage().'<br>'; //die($e->getMessage()); //'.$e->errorInfo().'<br>
}

Also, the user is created when checking in PhpMyAdmin->MariaDb, it shows the following credentials:

USERNAME: username_a

HOSTNAME: 127.0.0.1:3307

TYPE: wildcard:database_a

PRIVILEGES: SELECT,INSERT,UPDATE,DELETE

GRANT: No

The only response I'm getting is:

A complete
B complete
C complete
D Error:1045:
SQLSTATE[HY000] [1045] Access denied for user 'username_a'@'localhost' (using password: YES)

which is weird because C was never completed as well as D.

Thanks.

doodj7463
  • 27
  • 1
  • 6
  • That answer doesn't apply. I don't have an anonymous user @ localhost. Can u see this comment? – doodj7463 May 15 '20 at 12:57
  • It also doesn't explain why my table isn't getting created. @JayBlanchard – doodj7463 May 15 '20 at 12:59
  • Thanks tho @Jay Blanchard but I had a look and none of those seemed to help. Is there something in particular on that page you're inferring that perhaps I missed? – doodj7463 May 15 '20 at 13:04
  • The duplicate posted here is representative of a number of duplicates for the same issue. If you do not find a solution in the duplicate listed please search this site for the same error to reveal dozens of questions with the same issue and dozens of solutions for the problem. – Jay Blanchard May 15 '20 at 13:06
  • Thanks @ JayBlanchard. I have searched the site for a cpl of hours before posting though. I think my problem is unique because it's not creating the table correctly. I'll have another search for cpl hours and get back to you. – doodj7463 May 15 '20 at 13:11
  • The access denied error and the failure to create a table are two different issues. You may want to consider running the table creation as a standalone script to see what, if any, errors are generated. – Jay Blanchard May 15 '20 at 13:13
  • Thanks @ JayBlanchard. I gotthe table creation sorted and I got the table insert sorted with a hack but there's still the problem of: the error. The error says that 'username_a'@'localhost' was denied, but it should be 'username_a'@'127.0.0.1:3307'. The username_a@127.0.0.1:3307 was created correctly, but for some reason PDO is searching for username_a@localhost, which obviously was not created, and therefore access was denied. I'm having trouble for search terms in SO for this problem. Any thoughts? – doodj7463 May 15 '20 at 14:46
  • Do you have a hosts file on the server? – Jay Blanchard May 15 '20 at 14:55
  • It's a WAMP config so I found `httpd-vhosts.conf` and `httpd.conf`. If you're referring to mysql, I found the `host` table in the `mysql` database of my MariaDb in PhpMyAdmin. @JayBlanchard – doodj7463 May 15 '20 at 15:26
  • There is a hosts file somewhere on your system in which you can map localhost. https://superuser.com/questions/111685/how-can-i-specify-ip-and-ports-for-a-hostname-in-the-windows-hosts-file/505973 – Jay Blanchard May 15 '20 at 15:44

0 Answers0