Jump to content

php mysql Create Database On The Fly


Recommended Posts

Create a new database from information sent from a form.

In this example I use an external config file with global information

that is needed for proper execution. This can be changed to your settings...

 

The Script (create_db.php):

<?php
include("../config.php");



$con = mysql_connect($global_dbhost,$global_dblogin, $global_dbpass);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

$dbname = $_POST['databasename'];

if (mysql_query("CREATE DATABASE $dbname",$con))
  {
  echo "Database created";
  }
else
  {
  echo "Error creating database: " . mysql_error();
  }

mysql_close($con);
?>

 

The config file:

<?php
$global_dbType = 'mysql';
$global_dbhost = 'localhost';
$global_dbname = 'your_db_name';
$global_dblogin = 'your_username';
$global_dbpass = 'your_password';
$global_encoding = 'utf-8';
$global_rows_per_page = 20;
$global_pager_items = 10;
?>

 

The form Code:

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled 1</title>
</head>

<body>

<form method="post" action="create_db.php">
    <input name="databasename" type="text" style="width: 287px"><input name="Submit1" type="submit" value="submit"></form>

</body>

</html>

 

Link to comment
Share on other sites

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.   Paste as plain text instead

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