$connect = mysql_connect($host, $username, $password) or die(mysql_error());
mysql_select_db($database, $connect) or die(mysql_error());
Here is my table creating code:
$query="CREATE TABLE app (
ID int(8) DEFAULT'0' NOT NULL auto_increment,
Blah blah varchar(50) NOT NULL,
Something varchar(50) NOT NULL
I tried putting the $query into the main script (the database connect script) but it still won't create the table. I tried yesterday and created a table in that same database. But today it wouldn't work and I can't figure out why.
I don't want to manually create the table with PHPmyadmin, since I want to learn how to make a working-script that will do that for me.
So can anyone fix these script or tell me what I did wrong?
Try putting a
right after your failing query.
A question to ask: generally whenever I see php that is supposed to create tables it is a bad thing. The creation of tables should be a one-time thing as it has much more overhead than simply creating rows. Do you really need to be creating tables?
You can create a table with one name only once in any database. Since you are executing the query twice, at first execution it will work fine and while executing next time it won't create table resulting in error. If you want to create a table with same name, then you can use TEMPORARY keyword in create table syntax. This feature is available only in mySQL Version 3.23. If you are doing so, the temporary table is visible only to the current connection, and will be deleted automatically when the connection is closed. This means that two different connections can both use the same temporary table name without conflicting with each other or with an existing table of the same name. (The existing table is hidden until the temporary table is deleted.) Also by default, In database connection object, the COMMIT is set to TRUE. So the data connections what are the user trying will be committed. So by changing this priviliege setting also you can create table with same time ( COMMIT SET TO FALSE )