Creating a table fubar!

Started by Ardenn, December 08, 2003, 02:10:43 PM

Previous topic - Next topic

Ardenn

Can anyone tell me where I made my mistake?  

I am using this code

<?
//Connect to the database
$bg = mysql_connect ("localhost","root","") or die ("Could not connect to MySQL");
mysql_select_db(songs,$bg) or die("Could not find database");

//Create table
$createsongdata = "CREATE TABLE songdata (
ID INT(3) NOT NULL PRIMARY KEY AUTO_INCREMENT,
LEVEL INT(2) NOT NULL,
NAME CHAR(100) NOT NULL
);

//Let me know that table was created
$result = mysql_query($createsongdata);
if ($result) {echo "Table was created";}
else {echo "Table was NOT created!";}
?>


To attempt to create a table, but Im getting error message :

Parse error: parse error, unexpected T_STRING in c:\program files\easyphp\www\dbtest.php on line 15

When I run the script where line 15 is :

if ($result) {echo "Table was created";}
Ardenn // Traxxus
http://www.twinwand.com
D&D Play by Post Community Looking for Players and DM's

[Unknown]

NAME CHAR(100) NOT NULL
);

Should be:

NAME CHAR(100) NOT NULL
");

-[Unknown]

Ardenn

I dont see why Unknown.  Although I did try it and it generated another parse error on line 12 where I added the

");
Ardenn // Traxxus
http://www.twinwand.com
D&D Play by Post Community Looking for Players and DM's

SparkieGeek

Quote from: [Unknown] on December 08, 2003, 02:15:52 PM
NAME CHAR(100) NOT NULL
");
*cough*typo*cough*

NAME CHAR(100) NOT NULL
)";

Ardenn

LOL one little   Close Quote..  Arrg.. lol  I understand now.. I opened quote at the beginning and needed to close afterward.  Oh my head!
Ardenn // Traxxus
http://www.twinwand.com
D&D Play by Post Community Looking for Players and DM's

[Unknown]

#5
By the way, the code beautification police asked me to tell you this would look nicer :P:

<?php
// Connect to the database.
$bg = mysql_connect ('localhost', 'root', '') or die ('Could not connect to MySQL');
mysql_select_db('songs', $bg) or die('Could not find database');

// Create table
$result = mysql_query("
CREATE TABLE songdata (
ID INT(3) NOT NULL PRIMARY KEY AUTO_INCREMENT,
LEVEL INT(2) NOT NULL,
NAME CHAR(100) NOT NULL
)"
);

// Was the table created?
if ($result)
echo 'Table was created';
else
echo 'Table was NOT created!';

?>


-[Unknown]

SparkieGeek

#6
Would look even nicer if it was code that worked ;) ;)

Note the missing ) at the end of the CREATE TABLE statement ;D

PS I don't deliberately follow you round correcting your typos :-X
...the code just screams at me

[Unknown]

Still looks better... wasn't my fault... *looks around as if it wasn't.*  It was, uhh... the code beautification police's!

I always use TYPE=MyISAM at the end of my creates because if I do I don't forget the ending parenthesis ;).

-[Unknown]

Ardenn

Ok fair enough.  Im very new at this PHP stuff.  See below my name it even says PHP Newb!

Im killing myself of typo's apparently.   The reason I ask for help here is that eye's different from mine are able to see the mistakes easier.  I hope you all dont get to tired of me.  Im having problem with this code:



<?
//Connect to the database

$bg = mysql_connect ("localhost","root","") or die ("Could not connect to MySQL");
mysql_select_db(songs,$bg) or die("Could not find database");

//Create table
$createsongdata = "CREATE TABLE songdata (

ID INT(3) NOT NULL PRIMARY KEY AUTO_INCREMENT,
Name CHAR(100) NOT NULL,
Level INT(2) NOT NULL,
Skill CHAR(50) NOT NULL,
Desc CHAR(200) NOT NUL,
Loc CHAR(20) NOT NUL,
Cost CHAR(30) NOT NUL,
Cast_Time CHAR(30) NOT NUL,
Delay CHAR(30) NOT NUL,
Maxdur CHAR(30) NOT NUL,
Song_Radius CHAR(30) NOT NUL,
Song_Msg CHAR(250) NOT NUL,
Fade_Msg CHAR(250) NOT NUL,
Submitter CHAR(30) NOT NUL,
Server CHAR(30) NOT NUL
)";

//Let me know that table was created
$result = mysql_query($createsongdata);
if ($result) {
echo("Table created successfully");
} else {
echo("error when creating table");
}
?>


Help P L E A S E.. Whats wrong oh my head will explode...            KKKKKKKKKKKKAAAABBBBBBOOOOOOOOOOOOM!
Ardenn // Traxxus
http://www.twinwand.com
D&D Play by Post Community Looking for Players and DM's

SparkieGeek


Ardenn

#10
See!  I told you.. a fresh pair of eyes fixes the problem every time.. Duh! I feel stupid now!

Edit:  Changed the NUL to NULL but still didnt work.
Ardenn // Traxxus
http://www.twinwand.com
D&D Play by Post Community Looking for Players and DM's

SparkieGeek

hehe, it happens to all of us, as you say a fresh pair of eyes makes all the difference! :D

Good luck with the rest of your coding!

Ardenn

 Changed the NUL to NULL but still didnt work.
Ardenn // Traxxus
http://www.twinwand.com
D&D Play by Post Community Looking for Players and DM's

SparkieGeek

stick some single quotes around song

Ardenn

#14
Well I fixed it.. I didnt stick any single quotes around song.  Here is what I did do.

I took the base code :

$createsongdata = "CREATE TABLE songdata (
)";


Add added each entry and tested it one at a time.  Each time it created the table, I would drop the table and add the next line and recreate the table again.  It was a long process, and I never found a REAL error in the original file.  So I dont really know what I did wrong, which is somewhat frustrating.  It does work now though.

The bizarre thing I did find was that the line of code:

Desc CHAR(200) NOT NULL,

was generating an error.  I changed it to:

Description CHAR(200) NOT NULL,

and it worked.  I just dont know why.


Ardenn // Traxxus
http://www.twinwand.com
D&D Play by Post Community Looking for Players and DM's

SparkieGeek

#15
Well I'm glad you've got it working :)


mysql_select_db
(songs,$bg) or die("Could not find database");


should be:

mysql_select_db
('songs',$bg) or die("Could not find database");


(single quotes or double quotes, single is faster though)

because I can be 95% sure that you mean songs to be a string, and you're not referring to a constant. If you up the error_reporting level to E_ALL then you'll see a Warning about it.

Ardenn

Actually songs is not a string.  It is the name of the mysql database I am using.  That would make it a constant right?
Ardenn // Traxxus
http://www.twinwand.com
D&D Play by Post Community Looking for Players and DM's

SparkieGeek

#17
Quote from: Ardenn on December 08, 2003, 05:16:00 PM
Desc CHAR(200) NOT NULL,

was generating an error.  I changed it to:

Description CHAR(200) NOT NULL,

and it worked.  I just dont know why.

ahh of course. It's because DESC is a MySQL keyword/reserved name whatever you want to call it

SparkieGeek

Quote from: Ardenn on December 08, 2003, 05:27:53 PM
Actually songs is not a string.  It is the name of the mysql database I am using.  That would make it a constant right?
As far as PHP is concerned, it's a string. A constant would have to be defined by you previously in your code.

Something along the lines of:
<?php
define
('DATABASE_NAME', 'songs');
?>


then you could do this:
<?php
mysql_select_db
(DATABASE_NAME, $bg);
?>


Look at the php manual section on constants for more info

[Unknown]

For clarification here, this is a variable:

$variable

And this is a string:

'string'

Everything that is text in PHP is a string.  It can be in variables, or anywhere else... but it has to be in strings.  Variables can be strings, numbers, and other things.

When you say something like this:

echo text;

It works, silently, but you get a LARGE penalty in execution time for it. (comparitively.)  This is because it checks, finds no "text" constant, and then makes one for you with the value "text".

But, now look at this... what if you did this?

mysql_select_db(my database, $bg);

You'd get a parse error.  See, "my database" cannot be a constant, because constants can't have spaces... but, look, if you do it properly:

mysql_select_db('my database', $bg);

It works ;).  Being comfortable with strings is important - they aren't thre to make things harder, they are your friends ;).

-[Unknown]

Advertisement: