Creating MySQL Tables with PHP

3 posts Page 1 of 1
Contributors
User avatar
wrighty1986
VIP - Donator
VIP - Donator
Posts: 119
Joined: Sat Sep 12, 2009 11:55 am

Creating MySQL Tables with PHP
wrighty1986
Okay, so in the tutorial I will teach you how to set up a piece of code that inserts Tables and Data into a MySQL database

This could be used with an Installer for a piece of Web Software ect.

It's pretty basic stuff really.

We start off by telling PHP the details to your SQL database
Code: Select all
<?php

		//Host | Database User | Database Password
		mysql_connect("localhost", "", "") or die(mysql_error());
		
		//Database Name
		mysql_select_db("") or die(mysql_error());

mysql_query
After this we open our parameters and tell it to create a new table
Code: Select all
("CREATE TABLE name")
You would replace 'name' with the name you wish to call your table.

If you wish for your table to have a primary ID key within it, this is usually used when making a member table or post table ect, we would specify this code:
Code: Select all
id INT NOT NULL AUTO_INCREMENT,
The automatically put's the ID in every new record entered into the database

Now we can begin specifying all the tables we want to be created, and then close the parameters
Code: Select all
name VARCHAR(900), 
 description VARCHAR(900), 
 logo VARCHAR(900),
 keywords VARCHAR(900))")
Obviously not everything works the first time, so its important that we have a die statement at the end of ever SQL query
Code: Select all
or die(mysql_error());
Or if it was successful output a success message
Code: Select all
echo "System Information Table Created";

?>
You should now have code similar too
Code: Select all
<?php

mysql_query("CREATE TABLE info(
id INT NOT NULL AUTO_INCREMENT, 
PRIMARY KEY(id),
 name VARCHAR(900), 
 description VARCHAR(900), 
 logo VARCHAR(900),
 keywords VARCHAR(900))")
 or die(mysql_error());  

echo "System Information Table Created";

?>
Save the code to a .php file, upload it to a server and execute it!

Happy Programming!
Back Aegean sorry not been on i.v just been moving.
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: Creating MySQL Tables with PHP
mandai
Was this posted before?
Last edited by mandai on Thu Jan 12, 2012 11:13 pm, edited 1 time in total.
User avatar
rocky4126
VIP - Donator
VIP - Donator
Posts: 258
Joined: Mon Nov 16, 2009 7:39 pm

wrighty please give credit to the original coders who gave this out.
Rocky
Image
3 posts Page 1 of 1
Return to “Tutorials”