Jump to content


Photo

Query problem


  • Please log in to reply
8 replies to this topic

#1 ViralStef

ViralStef
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 17 July 2006 - 08:50 AM

Hi

I'm trying to generate a CREATE TABLE query:

$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));

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

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


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

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


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

What is wrong with my query?

Plz help
Thx

#2 Honoré

Honoré
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts
  • LocationAntwerp - Belgium

Posted 17 July 2006 - 09:05 AM

1) try
$createtable_query = "CREATE TABLE '$componentname' ('ID' INT(10) UNSIGNED NOT NULL AUTO_INCREMENT" . $table_string . ")";
mysql_query($createtable_query) or die(mysql_error());
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.

#3 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 17 July 2006 - 09:39 AM

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: ``
<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#4 ViralStef

ViralStef
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 17 July 2006 - 09:40 AM

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

#5 ViralStef

ViralStef
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 17 July 2006 - 09:50 AM

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!

#6 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 17 July 2006 - 09:55 AM

Well instead of
"CREATE TABLE '$componentname'
I think it should be
"CREATE TABLE `$componentname`

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.
<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#7 ViralStef

ViralStef
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 17 July 2006 - 10:01 AM

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:

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


But still the same error....

#8 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 17 July 2006 - 10:28 AM

This should work, you have to set ID to primary key because it is AUTO_INCREMENT:
CREATE TABLE `componenttest` (
`ID` int( 10 ) UNSIGNED NOT NULL AUTO_INCREMENT ,
`Name` varchar( 255 ) NOT NULL ,
PRIMARY KEY ( `ID` ) 
)

<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#9 ViralStef

ViralStef
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 17 July 2006 - 05:36 PM

Solved:d!
Indeed, primary key had to be set, and backtips had to be used!

thx a lot ;D




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users