Jump to content

Create a MySQL database using PHP


Lynds777

Recommended Posts

What database server are you using? MySQL, MsSQL, ... ?
You should use the exact same queries as you would use in your database server to create a database.

[code]
<?php
// If you're using MySQL
// -- Make sure your user you are using to log into the databaseserver has the proper rights to create a database and user !!!
$db = mysql_connect('yourHost', 'yourUser', 'yourPassword');
mysql_query('CREATE DATABASE mydatabasename');
....
?>
[/code]
Link to comment
Share on other sites

It's a mySQL db server.

I heard that some servers wont allow you to create a database in php, only through cPanel. I'm starting to think that may be the case for myself but I thought I'd ask here before I give up.

I have the following code:

[code]<?php
$con = mysql_connect("localhost","radioact_Lynds","Password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }


if (mysql_query("CREATE DATABASE radioact_testdb",$con))

  {
  echo "Database created";
  }
else
  {
  echo "Error creating database: " . mysql_error();
  }



mysql_select_db("radioact_testdb", $con);
$sql = "CREATE TABLE testtable
(
FirstName varchar(15),
LastName varchar(15),
Age int
)";[/code]

I get the message:
Error creating database: Access denied for user 'radioact_Lynds'@'localhost' to database 'radioact_testdb'
Link to comment
Share on other sites

I am the db admin. The server I use uses cPanel and all the databases I've made do far have been done through that. Each time I make a db I have to add myself as a user with all privileges. Your reply makes me convinced my server has me blocked from doing this through php script.

Thanks for your help
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.