I've just started working with PHP and MySQL, and I'm trying to create a table in MySQL. If I've got this right, I'm trying to do so by going to my host's SQL MyAdmin page and typing in

mysql_query("CREATE TABLE example(
id INT NOT NULL AUTO_INCREMENT, 
PRIMARY KEY(id),
 name VARCHAR(30), 
 age INT)")

in the query tab. I get an error saying that my syntax is wrong. Any advice?

Re: How do I create a table in a database? 80 80

I believe the PRIMARY statement needs to go at then end of all the column definitions

CREATE TABLE example (
  id int NOT NULL AUTO_INCREMENT,
  name varchar(30),
  age int,
  PRIMARY KEY (id)
)
Re: How do I create a table in a database? 80 80

I don't thing primarykey statement creates error,
I don't thing its is error in query, It may be #1046 - No database selected error,if so create database and select that

try with that

create database db_name;
use db_name;
CREATE TABLE example(
id INT NOT NULL AUTO_INCREMENT, 
PRIMARY KEY(id),
 name VARCHAR(30), 
 age INT)
Re: How do I create a table in a database? 80 80

I get an error saying that my syntax is wrong

What is the actual error message you are getting? Is it from mysql or from PHP?

the line of code you posted doesn't have a semicolon at the end, which will generate a syntax error in php if its like that in your page.

commented: The error message doesn't actually display an error code; very strange. +0
Re: How do I create a table in a database? 80 80

Try this code :
CREATE TABLE Example (
id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
name varchar(30) NOT NULL,
age int
);

Re: How do I create a table in a database? 80 80

First step is to create database.
Syntax is:
create database databasename;

Next step is creating table within this database.
Creating a table with name- table1 with five columns: id, name, address, salary, phoneno
Syntax is:
CREATE TABLE table1 ( id INT(6) , name VARCHAR(30) NOT NULL,
address VARCHAR(30) NOT NULL, salary int(10), phoneno int(10) NOT NULL );

HERE , NOT NULL means each row must contain a value for that column, null values are not allowed.
Create your Table with the columns as you required.

Re: How do I create a table in a database? 80 80

You should try this one:-
id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
name varchar(30) NOT NULL,
age int
);

Re: How do I create a table in a database? 80 80

hello patiodude
this might help you check it down below
CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)
if you still facing any error let me know

commented: Be timely. +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.