logo-dw

Go Back   Dreamweaver Club Forums > Hand Coders Forum > PHP
Register FAQ Members List Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Display Modes
Old 09-24-2011, 01:08 PM   #1
gigiloumill
gigiloumill's Avatar
 
Join Date: Apr 2009
Location: Miami, FL
Posts: 493
Default PHP script to create a sql database

Guys, does any one have a php script to create databases on the fly. I need to create several databases and I really don't want to create 1 field at a time. Thanks
__________________
Keeping your business connected.
www.starionmedia.com
gigiloumill is offline   Reply With Quote
Old 09-26-2011, 02:09 AM   #2
edbr
edbr's Avatar
 
Join Date: Aug 2005
Location: Bali
Posts: 11,176
Default

using PDO this may help. its for mysql lite but PDO can use many different database engines http://henryranch.net/software/ease-...h-php-and-pdo/
__________________
If you're happy and you know it shake your meds!
different style links examples

Flight / Hotel search
Free script download
Bali Villas
edbr is offline   Reply With Quote
Old 09-30-2011, 12:20 PM   #3
gigiloumill
gigiloumill's Avatar
 
Join Date: Apr 2009
Location: Miami, FL
Posts: 493
Default

Thanks, E...
__________________
Keeping your business connected.
www.starionmedia.com
gigiloumill is offline   Reply With Quote
Old 09-30-2011, 02:45 PM   #4
davidj
davidj's Avatar
 
Join Date: Sep 2005
Location: The Toon (newcastle upon Tyne)
Posts: 8,256
Default

PDO is an object which is engine non-specific. It does not create databases it just allows you to store connections and queries in objects to make your app more scalable and extendible. For example you wont use specific MYSQL commands in your queries so just by changing the connection string can connect to any database and your app will run.

What you will have to do is create your databases manually and then export the DDL for future reuse.
__________________
Would you like to learn PHP from me? Check out -> www.codezenith.co.uk
davidj is offline   Reply With Quote
Old 09-30-2011, 02:50 PM   #5
gigiloumill
gigiloumill's Avatar
 
Join Date: Apr 2009
Location: Miami, FL
Posts: 493
Default

Thanks guys..
__________________
Keeping your business connected.
www.starionmedia.com
gigiloumill is offline   Reply With Quote
Old 10-01-2011, 02:21 AM   #6
edbr
edbr's Avatar
 
Join Date: Aug 2005
Location: Bali
Posts: 11,176
Default

you can create tables within a database though, no?

i have never tried it to be honest but it is documented that you can, although it looks like creating tables and inserting data into a main database to me. In this case a database already created with permissions granted, in this case dogsDb
Code:
<?php
	try
	{
//open the database
	$db = new PDO('sqlite:dogsDb_PDO.sqlite');
	 
	//create the database
	$db->exec("CREATE TABLE Dogs (Id INTEGER PRIMARY KEY, Breed TEXT, Name TEXT, Age INTEGER)");   
	 
//insert some data...
	$db->exec("INSERT INTO Dogs (Breed, Name, Age) VALUES ('Labrador', 'Tank', 2);".
	"INSERT INTO Dogs (Breed, Name, Age) VALUES ('Husky', 'Glacier', 7); " .
	"INSERT INTO Dogs (Breed, Name, Age) VALUES ('Golden-Doodle', 'Ellie', 4);");
	 
	//now output the data to a simple html table...
	print "<table border=1>";
	print "<tr><td>Id</td><td>Breed</td><td>Name</td><td>Age</td></tr>";
	$result = $db->query('SELECT * FROM Dogs');
	foreach($result as $row)
	{
	print "<tr><td>".$row['Id']."</td>";
	print "<td>".$row['Breed']."</td>";
	print "<td>".$row['Name']."</td>";
	print "<td>".$row['Age']."</td></tr>";
	}
	print "</table>";
	 
// close the database connection
	$db = NULL;
	}
	catch(PDOException $e)
	{
	print 'Exception : '.$e->getMessage();
	}
	?>
__________________
If you're happy and you know it shake your meds!
different style links examples

Flight / Hotel search
Free script download
Bali Villas
edbr is offline   Reply With Quote
Old 10-04-2011, 10:16 AM   #7
davidj
davidj's Avatar
 
Join Date: Sep 2005
Location: The Toon (newcastle upon Tyne)
Posts: 8,256
Default

Yes you can create databases and tables using PHP but you don't have to use PDO. You can pass in any SQL using any MYSQL command as long as your connection has the rights to do that...

PHP Code:
mysql_query("CREATE DATABASE mydatabase"); 

What I think gigiloumill is after is a script which would just create db's to save him some work
but if you have to write the script you may as well just create the database as normal and save the ddl for future use.
__________________
Would you like to learn PHP from me? Check out -> www.codezenith.co.uk
davidj is offline   Reply With Quote
Old 10-04-2011, 10:48 AM   #8
edbr
edbr's Avatar
 
Join Date: Aug 2005
Location: Bali
Posts: 11,176
Default

ah right , im just a new convert to PDO and getting carried away with it
__________________
If you're happy and you know it shake your meds!
different style links examples

Flight / Hotel search
Free script download
Bali Villas
edbr is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 03:20 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Copyright 2006 DreamweaverClub.com