Jump to content

Help with Creating a MySQL table in PHP


John V

Recommended Posts

Hi,

 

I'm trying to create a table in PHP.  I can use phpmyadmin to do it but I can't create it in PHP.  I even copied the code generated in phpmyadmin and it still didn't work.  Here is the code I wrote:

 

<?php

mysql_connect("localhost", "familyon_john", "woosha") or die(mysql_error());

echo "Connected to MySQL<br />";

 

mysql_select_db("familyon_blog") or die(mysql_error());

echo "Connected to Database";

 

// Create a MySQL table in the selected database

$query="CREATE TABLE `familyon_blog`.`test3` (

`day2` DATE NOT NULL ,

`age` INT NOT NULL ,

`name` VARCHAR( 7 ) NOT NULL ,

PRIMARY KEY ( `day2` )

) TYPE=MyISAM"

or die(mysql_error()); 

 

echo "Table Created!";

 

?>

 

This is the code I copied from phpmyadmin:

 

CREATE TABLE `familyon_blog`.`test` (

`home` VARCHAR( 20 ) NOT NULL ,

`address` VARCHAR( 30 ) NOT NULL

) ENGINE = MYISAM

 

I looked at the post by Drags111 and incorporated those changes, but it still didn't work.  Here is the output I get when I run the script:

 

Connected to MySQL

Connected to DatabaseYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 7

 

Thanks for any help you can give me!

Link to comment
Share on other sites

Try like this

<?php
$cnn=mysql_connect("localhost","familyon_john","woosha");
$db=mysql_query("create database familyon_blog");
print" DATABASE familyon_blog CREATED <br>";

mysql_select_db("familyon_blog",$cnn); 

//---------------------- CREATE TABLE HERE ---------------------------------	
$qry=mysql_query ("CREATE TABLE test3 (
`day2` DATE NOT NULL ,
`age` INT NOT NULL ,
`name` VARCHAR( 7 ) NOT NULL ,
PRIMARY KEY ( `day2` )
) TYPE=MyISAM"
or die(mysql_error()); 
print"TABLE  test3 CREATED <br>";

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.