Jump to content

Syntax error

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
Guest_Jaan_*

Guest_Jaan_*
  • Guests
Anyway.. i have this problemo..

$CreateTables = "CREATE TABLE 'administrator' (

'uname' VARCHAR( 15 ),

'pword' VARCHAR( 15 ),

'email' VARCHAR( 50 ),

'lastlogin' VARCHAR( 25 )

)


CREATE TABLE 'files' (

'id' INT( 5 ) AUTO_INCREMENT ,

'filename' VARCHAR( 50 ),

'size' VARCHAR( 50 ),

'mime' VARCHAR( 50 ),

'created' VARCHAR( 50 ),

'lastedit' VARCHAR( 50 ),

PRIMARY KEY ( 'id' )

)


CREATE TABLE 'history' (

'id' INT( 5 ) AUTO_INCREMENT ,

'filename' VARCHAR( 50 ),

'created' VARCHAR( 50 ),

'lastedit' VARCHAR( 50 ),

'action' VARCHAR( 50 ),

PRIMARY KEY ( 'id' )

)

";

$CreateQuery = mysql_query($CreateTables);

Quote

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''administrator' ( 'uname' VARCHAR( 15 ), 'pword' VARCHAR( 15 ), 'email' VARCH' at line 1

i have no idea how to fix it..
maybe someone can help me :)

#2
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
Rather than using apostrophes around the column names, try using ticks.

$CreateTables = "CREATE TABLE `administrator` (
`uname` VARCHAR( 15 ),
`pword` VARCHAR( 15 ),
`email` VARCHAR( 50 ), 


#3
Guest_Jaan_*

Guest_Jaan_*
  • Guests

Quote

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE TABLE `files` ( `id` INT( 15 ) AUTO_INCREMENT , `filename` VARCHAR( 50 ' at line 8

$tables ="CREATE TABLE `administrator` (

`uname` VARCHAR( 15 ),

`pword` VARCHAR( 15 ),

`email` VARCHAR( 25 ),

`lastlogin` VARCHAR( 25 )

)


CREATE TABLE `files` (

`id` INT( 15 ) AUTO_INCREMENT ,

`filename` VARCHAR( 50 ),

`size` VARCHAR( 50 ),

`mime` VARCHAR( 50 ),

`created` VARCHAR( 50 ),

`lastedit` VARCHAR( 50 ),

PRIMARY KEY ( `id` )

)


 CREATE TABLE `history` (

`id` INT( 15 ) AUTO_INCREMENT ,

`filename` VARCHAR( 50 ),

`created` VARCHAR( 50 ),

`lastedit` VARCHAR( 50 ),

`action` VARCHAR( 50 ),

PRIMARY KEY ( `id` )

)";


$query = mysql_query($tables);

**** nothing works.. i was discussing it with Jordan also.. and we never found any solutions..

#4
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
ok, it took me about two hours but the answer was staring me right in the face.

Quote

mysql_query() sends an unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier .

And your SQL query should be this:
[highlight="SQL"]CREATE TABLE `administrator` (
`uname` VARCHAR( 15 ),
`pword` VARCHAR( 15 ),
`email` VARCHAR( 25 ),
`lastlogin` VARCHAR( 25 )
);

CREATE TABLE `files` (
`id` INT( 15 ) AUTO_INCREMENT ,
`filename` VARCHAR( 50 ),
`size` VARCHAR( 50 ),
`mime` VARCHAR( 50 ),
`created` VARCHAR( 50 ),
`lastedit` VARCHAR( 50 ),
PRIMARY KEY ( `id` )
);

CREATE TABLE `history` (
`id` INT( 15 ) AUTO_INCREMENT ,
`filename` VARCHAR( 50 ),
`created` VARCHAR( 50 ),
`lastedit` VARCHAR( 50 ),
`action` VARCHAR( 50 ),
PRIMARY KEY ( `id` )
);[/highlight]

Each time you create a table, it is a separate query. Also note your administrator table does not have an index. Thus you have two choices, you can break each table creation into its own query:

<?php
mysql_error());
$tables1 = "CREATE TABLE `administrator` (
`uname` VARCHAR( 15 ),
`pword` VARCHAR( 15 ),
`email` VARCHAR( 25 ),
`lastlogin` VARCHAR( 25 )
);";

$tables2 = "CREATE TABLE `files` (
`id` INT( 15 ) AUTO_INCREMENT ,
`filename` VARCHAR( 50 ),
`size` VARCHAR( 50 ),
`mime` VARCHAR( 50 ),
`created` VARCHAR( 50 ),
`lastedit` VARCHAR( 50 ),
PRIMARY KEY ( `id` )
);";

$tables2 = "CREATE TABLE `history` (
`id` INT( 15 ) AUTO_INCREMENT ,
`filename` VARCHAR( 50 ),
`created` VARCHAR( 50 ),
`lastedit` VARCHAR( 50 ),
`action` VARCHAR( 50 ),
PRIMARY KEY ( `id` )
);";

$query = mysql_query($tables1); 
$query = mysql_query($tables2); 
$query = mysql_query($tables3); 

?>

Or you can use PHP5's MySQL Improved [mysqli] extension, which might look like this:

$link = mysqli_connect("localhost", "uname", "pword");
mysqli_select_db($link, "test");
/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}


$tables ="CREATE TABLE `administrator` (
`uname` VARCHAR( 15 ),
`pword` VARCHAR( 15 ),
`email` VARCHAR( 25 ),
`lastlogin` VARCHAR( 25 )
);

CREATE TABLE `files` (
`id` INT( 15 ) AUTO_INCREMENT ,
`filename` VARCHAR( 50 ),
`size` VARCHAR( 50 ),
`mime` VARCHAR( 50 ),
`created` VARCHAR( 50 ),
`lastedit` VARCHAR( 50 ),
PRIMARY KEY ( `id` )
);

 CREATE TABLE `history` (
`id` INT( 15 ) AUTO_INCREMENT ,
`filename` VARCHAR( 50 ),
`created` VARCHAR( 50 ),
`lastedit` VARCHAR( 50 ),
`action` VARCHAR( 50 ),
PRIMARY KEY ( `id` )
);"; 

mysqli_multi_query($link, $tables) or die(mysqli_error($link));


#5
Guest_Jaan_*

Guest_Jaan_*
  • Guests
well i tried 'em all.. ;) didnt work..

<?php
mysql_error());
$tables1 = "CREATE TABLE `administrator` (
`uname` VARCHAR( 15 ),
`pword` VARCHAR( 15 ),
`email` VARCHAR( 25 ),
`lastlogin` VARCHAR( 25 )
);";

$tables2 = "CREATE TABLE `files` (
`id` INT( 15 ) AUTO_INCREMENT ,
`filename` VARCHAR( 50 ),
`size` VARCHAR( 50 ),
`mime` VARCHAR( 50 ),
`created` VARCHAR( 50 ),
`lastedit` VARCHAR( 50 ),
PRIMARY KEY ( `id` )
);";

$tables2 = "CREATE TABLE `history` (
`id` INT( 15 ) AUTO_INCREMENT ,
`filename` VARCHAR( 50 ),
`created` VARCHAR( 50 ),
`lastedit` VARCHAR( 50 ),
`action` VARCHAR( 50 ),
PRIMARY KEY ( `id` )
);";

$query = mysql_query($tables1); 
$query = mysql_query($tables2); 
$query = mysql_query($tables3); 

?> 
this one worked.. almost because i received this error:

Quote

Query was empty

dang.. wierd

#6
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
...the second $table2 should be $table3...

Also note md5 hashes are 32 characters, your password field only allows 15 characters. Are you storing your passwords in plain text?

#7
Guest_Jaan_*

Guest_Jaan_*
  • Guests

Sidewinder said:

...the second $table2 should be $table3...

Also note md5 hashes are 32 characters, your password field only allows 15 characters. Are you storing your passwords in plain text?

this password thingy is just temporary ;)