Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

MySQL creating a table


ghost's Avatar
0 0

I'm making a comments section for my site and I'm having a problem…

I can't create a new table if one doesn't exist… I can create one fine in small crappy test scripts where I don't need to worry about whether it already exists but I can't get one to work in a proper script and I think it has something to do with the fact that i need to check whether one already exists

I've tried both of the following:

{
	mysql_query( "CREATE TABLE " . $tablename . " ( name varchar(30), comment varchar(1000), number int )" ) or die( "Failed to create table" );
}```


```markupmysql_query( "CREATE TABLE IF NOT EXISTS" . $tablename . "( name varchar(30), comment varchar(1000), number int )" );```

I can send you the source if you need it... but I obviously don't want to send it to anyone untrustworthy in case I've left out something that could be exploited

ghost's Avatar
0 0

It's been a minute since I messed with PHP (meaning to get back into it), but couldn't you just execute a simple SELECT * FROM tblname to see if the table exists? In PHP, I think it's mysql_query("SELECT…") or die. But, instead of die, just run a non-query for your CREATE TABLE statement.

Of course, I'm not sure what you're doing this for, so I can't be more specific than that.

Edit: Now that I look back at your post, I think that executing a CREATE TABLE statement as a query in PHP might be the problem area. My main prog language right now is VB.Net, and I know that, in ADO.Net, you execute CREATE TABLE statements as a non-query. Not sure what the PHP equivalent of that would be, if there is one.