[RESOLVED] Create table not working!
Hi,
I'm using code I have used before to create a database! While I can create the database for some reason it won't allow me to create the table. This is the code I am using to create the database:
PHP Code:
<?php
//connect to server or exit
require_once("php/conroot.php");
//create database
$query = "CREATE DATABASE IF NOT EXISTS $dbDatabase";
if (mysql_query($query, $conn)) {
echo ("Database create query successful");
}
//select database
if (mysql_select_db($dbDatabase, $conn)) {
echo ("Database selection successful");
}else {
die ("Could not locate $dbDatabase database" .mysql_error());
}
//create table
$query = "CREATE TABLE IF NOT EXISTS $table (
'id' int(2) NOT NULL auto_increment unique,
'Nameochild' varchar(30) default NULL,
'Address' varchar(30) default NULL,
'Email' varchar(50) default NULL,
'Mobile' varchar(20) default NULL,
'Postcode' varchar(6) default NULL,
DOB' varchar(20) default NULL,
'Phone' varchar(10) default NULL,
'Medical' varchar(100) default NULL,
'Photo' varchar(1) default NULL,
'Guardian' varchar(20) default NULL,
'Date'(8) default NULL,
PRIMARY KEY ('id')
) TYPE=MyISAM";
if (mysql_query($query, $conn)) {
echo ("table $table query successful");
}
?>
Edit:
Here is the code for conroot.php:
PHP Code:
<?php
//connect to db
$conn = mysql_connect("localhost", "root", "");
if (!$conn) {
die("Connection failed: " .mysql_error());
}
// Database connection variables
$dbDatabase = "database";
$table = "table";
?>
Have I missed anything? I have looked over the code and cant see anything that might be wrong.
Thanks,
Nightwalker
Re: Create table not working!
I ran your query though phpMyAdmin. It gave this error:
Quote:
#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 ''id' int(2) NOT NULL auto_increment unique, 'Nameochild' varchar(30) default ' at line 2
Use ` instead of ' , should work
Re: Create table not working!
There are some format issues as well. You should proofread your query. To debug your code use something like this:
PHP Code:
mysql_query($query, $conn) or die(mysql_error());
Re: Create table not working!
I changed:
PHP Code:
'Date'(8) default NULL,
to
PHP Code:
Date varchar(8) default NULL,
Thanks for the help! That solved the problem.