I am helping a friend build as site and at a testing stage. it came to creating a database and i discovered he was a bity lost. i explained howto do it in phpadmin but i then decided to write a page or 2 to install it would be a nice project to do.
I got it done ( although messy) and would like to make it more generic for possible future use. So im risking your comments in the hope i can learn a bit more.

Ok first page 1
connects to sever (PDO )
creates database and a config file . so far so good

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>
<style>
label{ width:150px; float: left; clear:left;}
input{width:200px; float: left;}
</style><body>
<form action="" method="post">
Connect to server<br>
<label>Host name</label><input name="host" type="text"><br>
<label>user</label><input name="user" type="text"><br>
<label>Server pass word</label><input name="pass" type="text"><br>
<br>
<br>
New database and user <br>
<label>Database</label><input name="datab" type="text"><br>
<label>New user</label><input name="n_user" type="text"><br>
<label>New password</label><input name="n_pass" type="text"><br>
<label>create database</label><input name="go" type="submit" value="Submit"></form>

</body>
</html>
<?php           
$host=$_POST['host'];
$root=$_POST['user'];
$root_password=$_POST['pass'];
$user=$_POST['n_user'];
$pass=$_POST['n_pass'];
$db=$_POST['datab']; 
        $dbh = new PDO("mysql:host=$host", $root, $root_password);

        $dbh->exec("CREATE DATABASE `$db`;
                CREATE USER '$user'@'$host' IDENTIFIED BY '$pass';
                GRANT ALL ON `$db`.* TO '$user'@'$host';
                FLUSH PRIVILEGES;")   ;

           $data = " <?php ".
           '$host'.'='."\"".$host."\"". ";\n\r".
        '$uhost'.'='."\"".$db."\"". ";\n\r".
         '$uuser' .'='."\"".   $user."\"". ";\n\r".
         '$upass' .'='."\"".$pass."\"". ";\n\r".
';?>' ;

    $ret = file_put_contents('conn.php', $data);

?>
<br>
<a href="step2.php">go</a></body>

file 2 uses the config file i created for the new connection,

where i have the exec)....create table
The second file ands a table and some dummy data.
I would like to perhaps search for a file so $_POST...whatever added to a form. Is this possible? i have searched but cant find a reference.

its basic and does the job but would be nice to expand. maybe check connection before submitting form.
Also this is for a testing server however it occurs that live it would name a database and user as [SERVER]_database
and [SERVER]_user, and im not sure how to approach that. Obviously a bit out of my depth but trying to swim :)

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>
<body>
<?php  
include 'conn.php';
    try {
      $dbh= new PDO("mysql:host=localhost;dbname=$uhost",$uuser,$upass);
        $dbh->exec("        

                CREATE TABLE IF NOT EXISTS `item` (
                  `id` int(3) NOT NULL AUTO_INCREMENT,
                  `name` varchar(100) NULL,
                  `country` varchar(100)NULL,
                  `proof` varchar(100) NULL,
                  `age` varchar(100)  NULL,
                  `type` varchar(100) NULL,
                  `grade` varchar(100) NULL,
                  `description` mediumtext  NULL,
                  `flavour` varchar(100) NULL,
                  `image` varchar(100) DEFAULT NULL,
                  PRIMARY KEY (`id`),
                  KEY `id` (`id`)
                ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;          

INSERT INTO `item` (`id`, `name`, `country`, `proof`, `age`, `type`, `grade`, `description`, `flavour`, `image`) VALUES
(1, 'yingyang', 'japan', '70', '1', 'any', '60', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', 'camel piss', 'images/bottle.jpg'),
(2, 'tartan lion', 'thailand', '40', '2', 'any', '30', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam auctor lorem pharetra lacus convallis commodo eu iaculis lectus. Suspendisse quis eros varius, consequat arcu vel, fermentum nisl.', 'chemical soup', 'images/bottle.jpg'),
(3, 'island wee Dram', 'scotland', '40', '25', 'single', '40', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam auctor lorem pharetra lacus convallis commodo eu iaculis lectus. Suspendisse quis eros varius, consequat arcu vel, fermentum nisl. Vestibulum accumsan scelerisque quam.Donec at lacus eget quam es porttitor turpis eu, cursus molestie metus.', 'peat', 'images/bottle.jpg'),
(4, 'Pog Mahon', 'scotland', '42', '3', 'blended', '90', ' Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam auctor lorem pharetra lacus convallis commodo eu iaculis lectus. Suspendisse quis eros varius, consequat arcu vel, fermentum nisl. ', 'coek', 'images/bottle.jpg');
");
    } catch (PDOException $e) {
        die("DB ERROR: ". $e->getMessage());
    }

whoops done it and feeling bit embarrassed

$dbh->exec( $create)
tied myself in Knots. I would still appreciate some clues to advance this a bit

you can try creating functions that will sequentially handle the steps.

example,

function firstStep()
{
    /* create database here */

    /* if database has been created */

    /* return true; */

 }

 function secondStep()
 {
     /* insert dummy data */

 }

check if ready for the second step..

if(firstStep()){

    /* do second function */
    secondStep();
 }

you can also do 1 - 2

if(firstStep() && secondStep()){

    echo 'Success';

}

Another good alternative is chaining methods, if you know how to code in Object oriented..

commented: +rep for mentioning chaining - a very underused method +15
commented: Yh, coding in OOP is great, chaining. thumbs up +0

it did occour that a lass might be good as im thinking of this as reusable but im only on nodding terms with OOP, might be a good reason to go that way. i;d like to get to gripos with thinks like connection testing and how oit would work with shared hosting though before trying to struggle ( again ) with OOP.
Thank you for respnse btw :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.