Create MySql table via php?
Posted: Sun Sep 15, 2002 11:20 pm
Hey,
Glad I found this forum.
I just started with php/MySQL this week after a year or two of knowing I "should" be getting into it.
I'm doing well, but have a hitch I can't get around.
It seems that there are no mysql_whatever statements that will create a MySQL table. I have seen how to create a new database, how to append and delete and modify tables, but nothing on how to create a new table within a database.
I tried mysql_query(), but was unsuccessful. When I copied the exact query into PhpMyAdmin using copy and paste, it created the table just fine. I got no error messages or messages of any kind, but when I try to create a new table, it never shows up.
My SAMS Learning PHP in 24 Hours book shows nothing that allows me to do it, other than creating the table within MySQL.
I'd find it hard to believe that it just can't be done... any help would be appreciated!
By the way... here's the code snippet I'm using to try to create the table.... I connect ok, and when I just try to add data to the table (after I create it in PhpMyAdmin) it works fine.....:
$link = mysql_connect($server, $un, $pw);
if(! $link)
die("Couldn't connect to MySQL.");
print "<p><p>Successfully connected to MySQL server.<p><p>";
mysql_query( "CREATE TABLE students
(id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id),
lname VARCHAR (40),
fname VARCHAR (40) ,
address VARCHAR (40),
city VARCHAR (40),
province VARCHAR (5),
postalcode VARCHAR(6),
age TINYINT (2),
sex VARCHAR (1),
grade TINYINT (1))", $link );
I also tried it with a ; after the grade field, before the last ).... the book says to leave the ; off the query when using mysql_query.
Glad I found this forum.
I just started with php/MySQL this week after a year or two of knowing I "should" be getting into it.
I'm doing well, but have a hitch I can't get around.
It seems that there are no mysql_whatever statements that will create a MySQL table. I have seen how to create a new database, how to append and delete and modify tables, but nothing on how to create a new table within a database.
I tried mysql_query(), but was unsuccessful. When I copied the exact query into PhpMyAdmin using copy and paste, it created the table just fine. I got no error messages or messages of any kind, but when I try to create a new table, it never shows up.
My SAMS Learning PHP in 24 Hours book shows nothing that allows me to do it, other than creating the table within MySQL.
I'd find it hard to believe that it just can't be done... any help would be appreciated!
By the way... here's the code snippet I'm using to try to create the table.... I connect ok, and when I just try to add data to the table (after I create it in PhpMyAdmin) it works fine.....:
$link = mysql_connect($server, $un, $pw);
if(! $link)
die("Couldn't connect to MySQL.");
print "<p><p>Successfully connected to MySQL server.<p><p>";
mysql_query( "CREATE TABLE students
(id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id),
lname VARCHAR (40),
fname VARCHAR (40) ,
address VARCHAR (40),
city VARCHAR (40),
province VARCHAR (5),
postalcode VARCHAR(6),
age TINYINT (2),
sex VARCHAR (1),
grade TINYINT (1))", $link );
I also tried it with a ; after the grade field, before the last ).... the book says to leave the ; off the query when using mysql_query.