Creating a database

Hi,

I am building a CMS from scratch and my first step is the installation. I would like to create the database automatically on the install page (something like Fantastico) but I don’t know how to do that.

I know how to create a database using the following code, but that requires the db host, db username and db password information. Fantastico installs a script (such as WordPress) without bothering with that details. Do you have any ideas how to do that?

<?php
$con = mysql_connect("localhost","db_username","db_password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

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

mysql_close($con);
?>

you can run checks to see if the tables exist on the initial load and define a constant or variable to determine if it needs to run the tables installation or not, an older (imo better) technique is create an install file and delete it after the site is installed

but those are just my opinions

Please note that it is often not possible to create databases on shared hosting - hosts don’t give permission for this.

Fantastico does it differently: it’s a part of cPanel and already has access to your credentials.

I see, that means I will omit the database creation and just get the database details on the install page. Thanks for the comment.