Log in

View Full Version : Creating a mysql table with php



big-dog1965
10-04-2007, 02:25 AM
How do I make a PHP file creat this table in mysql. If I put this directly into sql it creats the table but I want a php file to do it by running it in the browser.

<?php
CREATE TABLE `wichita_frgn1`.`Registrations` ( `id` int( 10 ) unsigned NOT NULL auto_increment ,
`First_Name` text NOT NULL ,
`Last_Name` text NOT NULL ,
`Address` text NOT NULL ,
`City` text NOT NULL ,
`State` text NOT NULL ,
`Zip_Code` text NOT NULL ,
`Home_Phone` text NOT NULL ,
`Cell_Phone` text NOT NULL ,
`E_Mail` text NOT NULL ,
`Membership_No` text NOT NULL ,
`Roar_No` text NOT NULL ,
`First_Class` text NOT NULL ,
`Transmitter_Frequency` text NOT NULL ,
`Transponder_No` text NOT NULL ,
`Track_Transponder` text NOT NULL ,
`Second_Class` text NOT NULL ,
`Transmitter_Frequency2` text NOT NULL ,
`Transponder_No2` text NOT NULL ,
`Track_Transponder2` text NOT NULL ,
`Ability_Skill_Level` text NOT NULL ,
`Race_Date` date NOT NULL default '0000-00-00',
`Track` text NOT NULL ,
`IP_Address` text NOT NULL ,
`Date_Posted` text NOT NULL ,
PRIMARY KEY ( `id` ) ,
UNIQUE KEY `id_2` ( `id` ) ,
KEY `id` ( `id` ) ) ENGINE = MyISAM DEFAULT CHARSET = latin1 COMMENT = 'Created by Big-Dog;
?>

djr33
10-04-2007, 02:50 AM
<?php
$host = 'localhost'; //or url
$user = 'me';
$pass = 'pass'; //note this is secure on your server in the php file
$conn = mysql_connect($host,$user,$pass);
$query = 'CREATE TABLE .... STUFF';
mysql_query($query);
?>http://www.php.net/manual/en/function.mysql-connect.php

big-dog1965
10-04-2007, 04:23 AM
I get this error
Parse error: syntax error, unexpected T_LNUMBER in /home/public_html/Registration/creattable.php on line 28
which is `Race_Date` date NOT NULL default '0000-00-00',

<?php
$username="MYUSER";
$password="MYPASS";
$database="MYDB";

$conn = mysql_connect(localhost,$username,$password);
$query = 'CREATE TABLE `wichita_frgn`.`Registrations` ( `id` int( 10 ) unsigned NOT NULL auto_increment ,
`First_Name` text NOT NULL ,
`Last_Name` text NOT NULL ,
`Address` text NOT NULL ,
`City` text NOT NULL ,
`State` text NOT NULL ,
`Zip_Code` text NOT NULL ,
`Home_Phone` text NOT NULL ,
`Cell_Phone` text NOT NULL ,
`E_Mail` text NOT NULL ,
`Membership_No` text NOT NULL ,
`Roar_No` text NOT NULL ,
`First_Class` text NOT NULL ,
`Transmitter_Frequency` text NOT NULL ,
`Transponder_No` text NOT NULL ,
`Track_Transponder` text NOT NULL ,
`Second_Class` text NOT NULL ,
`Transmitter_Frequency2` text NOT NULL ,
`Transponder_No2` text NOT NULL ,
`Track_Transponder2` text NOT NULL ,
`Ability_Skill_Level` text NOT NULL ,
`Race_Date` date NOT NULL default '0000-00-00',
`Track` text NOT NULL ,
`IP_Address` text NOT NULL ,
`Date_Posted` text NOT NULL ,
PRIMARY KEY ( `id` ) ,
UNIQUE KEY `id_2` ( `id` ) ,
KEY `id` ( `id` ) ) ENGINE = MyISAM DEFAULT CHARSET = latin1 COMMENT = 'Created by big dog'
';
mysql_query($query);
?>


[MOD EDIT: DO NOT POST SENSITIVE DATA!! Removed. We don't need the exact password, etc. to help you and it is a HUGE security risk...]

tech_support
10-04-2007, 04:34 AM
You need to escape the quotes first.


<?php
$username="*****************"; //Replace the asterisks with your username.
$password="*****************"; //Replace the asterisks with your password.
$database="*****************"; //Replace the asterisks with your database.

$conn = mysql_connect(localhost,$username,$password);
$query = 'CREATE TABLE `wichita_frgn`.`Registrations` ( `id` int( 10 ) unsigned NOT NULL auto_increment ,
`First_Name` text NOT NULL ,
`Last_Name` text NOT NULL ,
`Address` text NOT NULL ,
`City` text NOT NULL ,
`State` text NOT NULL ,
`Zip_Code` text NOT NULL ,
`Home_Phone` text NOT NULL ,
`Cell_Phone` text NOT NULL ,
`E_Mail` text NOT NULL ,
`Membership_No` text NOT NULL ,
`Roar_No` text NOT NULL ,
`First_Class` text NOT NULL ,
`Transmitter_Frequency` text NOT NULL ,
`Transponder_No` text NOT NULL ,
`Track_Transponder` text NOT NULL ,
`Second_Class` text NOT NULL ,
`Transmitter_Frequency2` text NOT NULL ,
`Transponder_No2` text NOT NULL ,
`Track_Transponder2` text NOT NULL ,
`Ability_Skill_Level` text NOT NULL ,
`Race_Date` date NOT NULL default \'0000-00-00\',
`Track` text NOT NULL ,
`IP_Address` text NOT NULL ,
`Date_Posted` text NOT NULL ,
PRIMARY KEY ( `id` ) ,
UNIQUE KEY `id_2` ( `id` ) ,
KEY `id` ( `id` ) ) ENGINE = MyISAM DEFAULT CHARSET = latin1 COMMENT = 'Created by big dog'
';
mysql_query($query);
?>

djr33
10-04-2007, 04:35 AM
You must escape quotes of the same type as those surrounding the string, ie:

'don\'t';

Add a backslash before those offending quotes around the 00-000-00 thing.

big-dog1965
10-04-2007, 05:03 AM
now I get
Parse error: syntax error, unexpected T_STRING in /home/public_html/Registration/creattable.php on line 34
which is
KEY `id` ( `id` ) ) ENGINE = MyISAM DEFAULT CHARSET = latin1 COMMENT = 'Created by big dog'

djr33
10-04-2007, 05:38 AM
That line needs to be escaped as well.

Twey
10-04-2007, 12:05 PM
Perfect for a heredoc:
$query = <<<EOQ
CREATE TABLE
`wichita_frgn1`.`Registrations` (
`id` int( 10 ) unsigned NOT NULL auto_increment,
`First_Name` text NOT NULL,
`Last_Name` text NOT NULL,
`Address` text NOT NULL,
`City` text NOT NULL,
`State` text NOT NULL,
`Zip_Code` text NOT NULL,
`Home_Phone` text NOT NULL,
`Cell_Phone` text NOT NULL,
`E_Mail` text NOT NULL,
`Membership_No` text NOT NULL,
`Roar_No` text NOT NULL,
`First_Class` text NOT NULL,
`Transmitter_Frequency` text NOT NULL,
`Transponder_No` text NOT NULL,
`Track_Transponder` text NOT NULL,
`Second_Class` text NOT NULL,
`Transmitter_Frequency2` text NOT NULL,
`Transponder_No2` text NOT NULL,
`Track_Transponder2` text NOT NULL,
`Ability_Skill_Level` text NOT NULL,
`Race_Date` date NOT NULL default '0000-00-00',
`Track` text NOT NULL,
`IP_Address` text NOT NULL,
`Date_Posted` text NOT NULL,
PRIMARY KEY ( `id` ),
UNIQUE KEY `id_2` ( `id` ),
KEY `id` ( `id` )
)
ENGINE = MyISAM
DEFAULT CHARSET = latin1
COMMENT = 'Created by Big-Dog'
EOQ;
?>

big-dog1965
10-05-2007, 01:11 AM
THANKS
I got it to work