Page 1 of 1
Is it possible to create tables on a MySQL database using
Posted: Fri Apr 26, 2002 4:49 pm
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?
Posted: Fri Apr 26, 2002 5:14 pm
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.
Posted: Fri Apr 26, 2002 5:59 pm
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.
Posted: Fri Apr 26, 2002 6:29 pm
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.
Posted: Fri Apr 26, 2002 6:35 pm
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?
Posted: Fri Apr 26, 2002 6:55 pm
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
Posted: Sat Apr 27, 2002 7:06 am
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