CREATE DATABASE

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Parody
Forum Contributor
Posts: 252
Joined: Fri May 06, 2005 7:06 pm
Location: Great Britain

CREATE DATABASE

Post by Parody »

I am trying to use code to create a mysql database and some tables in it.

I have 2 files

File 1:

Code: Select all

<?php
$user="user";
$password="password";
$database="users";
mysql_connect("localhost",$user,$password);
mysql_query("CREATE DATABASE $database");
mysql_close();
?>
File 2:

Code: Select all

<?php
$user="user";
$password="password";
$database="users";
mysql_connect("localhost",$user,$password);
mysql_select_db($database)or die( "Unable to select database");
mysql_query("CREATE TABLE users (username varchar(40),password varchar(50),regdate varchar(20),email varchar(100),character varchar(2),PRIMARY KEY (username))");
mysql_query("CREATE TABLE stats (username varchar(40),character int(2) DEFAULT '01' NOT NULL,health int(3) DEFAULT '100' NOT NULL,money int(100) DEFAULT '2000' NOT NULL,rank int(3) DEFAULT '01' NOT NULL,level int(2) DEFAULT '001' NOT NULL,respect int(3) DEFAULT '001' NOT NULL,house int(2) DEFAULT '01',cstat int(2) DEFAULT '01' NOT NULL,gstat int(3) DEFAULT '001' NOT NULL,status int(1) DEFAULT '1' NOT NULL,jail int(1) DEFAULT '0' NOT NULL,gtatime NOT NULL,crimetime NOT NULL,octime NOT NULL,jailtime NOT NULL,PRIMARY KEY (username))");
mysql_close();
?>
It doesnt come up with any errors but I looked on my PHPMYADMIN and there are no entries. Any Suggestions :)
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

use mysql_error to recieve error messages...
Parody
Forum Contributor
Posts: 252
Joined: Fri May 06, 2005 7:06 pm
Location: Great Britain

Post by Parody »

It worked, thanks, it told me that my user I was using didn't have the right priveleges, so I changed that and it worked :) Thanks.

Sorry to ask more questions, but when I use the same error check in 'FILE 2' I get this error:

Code: Select all

1064: 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 'character int(2) DEFAULT '01' NOT NULL,health int(3) DEFAULT '10
I understand that I have done something wrong in the code :oops: , where do I find the mysql manuals?

Can anyone give a more descriptive answer to what error 1064 is please? :D
Parody
Forum Contributor
Posts: 252
Joined: Fri May 06, 2005 7:06 pm
Location: Great Britain

Post by Parody »

Anyone? :D :D :D :D :?:
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

character is an unknown column type... should be (var)char.

http://dev.mysql.com/doc/mysql/en/char.html
Parody
Forum Contributor
Posts: 252
Joined: Fri May 06, 2005 7:06 pm
Location: Great Britain

Post by Parody »

What does it need to look like?

I didnt really understand that page. Is it because character has a meaning in php? I'm trying to use it as a column name and it is getting confused?

Or is it something else? :oops:
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Idd, character is a reserved word. If you insist on using it, you need to use backticks to escape it...
mysql> create table test (character char(10) not null);
ERROR 1064: 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 'character char(10) not null)' at line 1

mysql> create table test (`character` char(10) not null);
Query OK, 0 rows affected (0.05 sec)
Parody
Forum Contributor
Posts: 252
Joined: Fri May 06, 2005 7:06 pm
Location: Great Britain

Post by Parody »

I changed the word character to something different and it worked :D .

Thanks
Post Reply