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]
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'
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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.