Jump to content

Archived

This topic is now archived and is closed to further replies.

ViralStef

Query problem

Recommended Posts

Hi

I'm trying to generate a CREATE TABLE query:

[code]$createtable_query = "CREATE TABLE '$componentname' ('ID' INT(10) UNSIGNED NOT NULL AUTO_INCREMENT" . $table_string . ")";
$createtable_result = mysql_query($createtable_query) or die(mysql_error($createtable_query));[/code]

$table_string is generated by the previous script.
When I print $createtable_query, I get this result (just an example):

[quote]CREATE TABLE 'componenttest' ('ID' INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, 'Name' VARCHAR(255) NOT NULL)[/quote]

That looks like a correct query.
Although, PHP warns:

[quote]Warning: mysql_error(): supplied argument is not a valid MySQL-Link resource in c:\program files\easyphp1-8\www\cms\adminpanel\components\addcomponent.php on line 128[/quote]

(Line 128 is "$createtable_result = ... ")

What is wrong with my query?

Plz help
Thx

Share this post


Link to post
Share on other sites
1) try
[code]
$createtable_query = "CREATE TABLE '$componentname' ('ID' INT(10) UNSIGNED NOT NULL AUTO_INCREMENT" . $table_string . ")";
mysql_query($createtable_query) or die(mysql_error());
[/code]
Your query should not return a result set for a CREATE TABLE command.
2) AUTO_INCREMENT on a VARCHAR column is probably not what you want.

Share this post


Link to post
Share on other sites
I THINK the problem is your quoting, as far as I know single quotes are only used for literal quoting of data. When you are quoting resource names such as database and tables I think you use backticks: ``

Share this post


Link to post
Share on other sites
Hi

I tried it without variable declaration, but it didn't work either.
Indeed, I don't want AUTO_INCREMENT on a varchar column, but does my script have that result?

Thx

Share this post


Link to post
Share on other sites
What do you mean with backtips? Are that quotes that are not straight (like in PHPMyAdmin)? I tried this by copying those quotes from PHPMyAdmin into my script. I don't know how to type them. But I tried it, and it didn't work either:(!

thx!

Share this post


Link to post
Share on other sites
Well instead of [code]"CREATE TABLE '$componentname'[/code]
I think it should be [code]"CREATE TABLE `$componentname`[/code]

For me, the backticks is in the top left of the keyboard, nothing happens when I press the button once, I have to press it twice and I get two backticks.

Share this post


Link to post
Share on other sites
Hm, I can't find those backtips on my keyboard, but I can copy them for PHPMyAdmin.
So, now I tried it with backtips and I get the following print result:

[quote]CREATE TABLE `componenttest` (`ID` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, `Name` VARCHAR(255) NOT NULL)[/quote]

But still the same error....

Share this post


Link to post
Share on other sites
This should work, you have to set ID to primary key because it is AUTO_INCREMENT:
[code]
CREATE TABLE `componenttest` (
`ID` int( 10 ) UNSIGNED NOT NULL AUTO_INCREMENT ,
`Name` varchar( 255 ) NOT NULL ,
PRIMARY KEY ( `ID` )
) [/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.