0

I'm trying to create a database if it does not exist. Code is here:

<?php
// Connect to MySQL
$link = mysql_connect('host', 'user', 'pw');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}

// Make studentdb the current database
$db_selected = mysql_select_db('StudentDB', $link);

if (!$db_selected) {
  // If we couldn't, then it either doesn't exist, or we can't see it.
  $sql = 'CREATE DATABASE StudentDB';

  if (mysql_query($sql, $link)) {
      echo "Database StudentDB created successfully\n";
  } else {
      echo 'Error creating database: ' . mysql_error() . "\n";
  }
}

mysql_close($link);
?>

I'm getting this error:

Error creating database: Access denied for user 'myusernamehere' to database 'StudentDB'

the user has dba permissions...I'm guessing this is a permission error for my user....how can I give the user permission through the script and make this script work?

Kenny
  • 59
  • 1
  • 7
  • You can't through this code, you'd need to `grant` your user permissions to create databases with your root user (or another user that has grant access.) – Jon Apr 02 '13 at 02:45
  • 1
    [Please, don't use `mysql_*` functions](http://stackoverflow.com/q/12859942/1190388) in new code. They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the red box? Learn about prepared statements instead, and use [tag:PDO] or [tag:MySQLi]. – hjpotter92 Apr 02 '13 at 02:45

2 Answers2

0

the users you are trying to use have no priviledges to use CREATE

try to switch to another user that have all the priviledges needed on manipulting the database or add the needed priviledges to the user you are using

Jhonathan H.
  • 2,734
  • 1
  • 19
  • 28
  • I've created a user with dba....inside of phpmyadmin I'm simply trying CREATE DATABASE test; and I'm still getting the error permission denied for user – Kenny Apr 02 '13 at 03:12
0

it happend to me and the following solved the problem:

  1. first before using code to editing the database go to the main localhost/index.php page then choose phpMyAdmin database manager
  2. after than make sure that you are loged in as admin
Poula Adel
  • 609
  • 1
  • 10
  • 33