Is it possible to create tables on a MySQL database using

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Is it possible to create tables on a MySQL database using

Post by Jim »

...PHP?

Or do I have to upload SQL files or whatever. It seems it's possible to create tables on the database. Doesn't phpBB do it?

If it IS possible, would you mind showing me where I might learn how to do it?

And if you could, might you direct me to some place that teaches you how to input information into a table using PHP?
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

yes you can use php to create tables.

Code: Select all

<?php
$database = mysql_connect('localhost', 'username','password');
mysql_create_db('db_name', $database);
mysql_select_db('db_name);
$query = "CREATE TABLE new_table (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, new_col VARCHAR(25))";
$result = mysql_query($query);
$axeit = mysql_drop_db('db_name');
?>

that should work, if anyone sees a mistake in it then plz fix it.


go to http://www.hotscripts.com/Detailed/4315.html, thay have some tuts for php, try that tut.
hope that helps.
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Post by Jim »

Thanks amigo :)

It worked. Now I just have to figure what all the stuff means...

id INT NOT NULL AUTO_INCREMENT PRIMARY KEY

means nothing to me...

*edit*

Also, if I want to create a signup kinda form and save all the information on this database, how do I make sure every person who input his/her information input it into a new field.

Know what I mean? I don't want every input to overwrite other input. I want each to have it's own id if that's the word.
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

here, lemme break that down for ya
SQL fragment: id INT NOT NULL AUTO_INCREMENT PRIMARY KEY

creates a field named 'id' of type INT. it is set so as not to allow NULL values. every time you insert a new row this field will generate a unique number in sequence (1,2,3,4,5,6,7,8) with each subsequent insert. it isthen made the primary indexing key (items in this field must be unique and will automaticlly be indexed).

hope that clarifys it for you.
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Post by Jim »

Indeed it does.

Thanks a lot :)

One problem though... when I try to create an entr into a table using php, if I don't define the value for "id" I get an error saying something like "values do not match number of fields". How do I fix that?
User avatar
sam
Forum Contributor
Posts: 217
Joined: Thu Apr 18, 2002 11:11 pm
Location: Northern California
Contact:

Post by sam »

You can do too things. First is to specify what fileds to insert into:

Code: Select all

INSERT INTO table ('name','age','height') VALUES ('Sam McKone','20','6 feet 5 inches');
The second is to include the value but with a random or null value:

Code: Select all

INSERT INTO table VALUES ('0','Sam McKone','20','6 feet 5 inches');
Hope that helps.

Cheers Sam
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Since your id field is auto-incrementing you can use the following SQL statement:

Code: Select all

INSERT INTO table VALUES (NULL,'Sam McKone','20','6 feet 5 inches');
Because you set the id field to NULL, MySQL will just add 1 to the previous id value in the table to get the value for the new record (ie. auto-increment the field).

Mac
Post Reply