PDA

Click to See Complete Forum and Search --> : [RESOLVED] Create table not working!


Nightwalker83
Feb 20th, 2010, 12:27 AM
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
//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
//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

dclamp
Feb 20th, 2010, 02:50 AM
I ran your query though phpMyAdmin. It gave this 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 ''id' int(2) NOT NULL auto_increment unique, 'Nameochild' varchar(30) default ' at line 2

Use ` instead of ' , should work

dclamp
Feb 20th, 2010, 03:00 AM
There are some format issues as well. You should proofread your query. To debug your code use something like this:


mysql_query($query, $conn) or die(mysql_error());

Nightwalker83
Feb 20th, 2010, 06:03 AM
I changed:


'Date'(8) default NULL,


to


Date varchar(8) default NULL,


Thanks for the help! That solved the problem.