create tables and fields

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
pauls74462
Forum Newbie
Posts: 16
Joined: Tue Jun 10, 2008 8:13 pm

create tables and fields

Post by pauls74462 »

I tried the below code to create tables and fields, not working I think according to the Google help have the syntax correct.

Anyone see what I'm doing wrong?
I get this error:
Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/MyServer/public_html/MyFolder/CreateTables.php on line 20
If I comment out after "echo connection made" I get the message that connection is made. I commented out line 20 and the error was moved down one line.

Code: Select all

 
<?php
 
 
$hostname  = "localhost";
$dbusername  = "MyServer_MyUser";
$dbpassword  = "**************";
$dbname  = "MyServer_My_db";
 
 
$con1 = mysql_connect($hostname,$dbusername,$dbpassword);
 
if (!$con1)
  {
  die('Could not connect: ' . mysql_error());
  }
echo "Connected made"
 
 
mysql_select_db($dbname, $con1);  // Line 20
CREATE TABLE My_db
(
  Level varchar(5) NOT NULL,
  QN int(2) NOT NULL,
  Question varchar(50) NOT NULL,
  Answer varchar(50) NOT NULL,
  Wrong1 varchar(50) NOT NULL,
  Wrong2 varchar(50) NOT NULL,
  Wrong3 varchar(50) NOT NULL,
  HINT varchar(50) NOT NULL,
  ImagePath1 varchar(50) NOT NULL,
  ImagePath2 varchar(50) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
or die(mysql_error());  
 
echo "Table Created!";
 
 
?>
 
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: create tables and fields

Post by jaoudestudios »

You have a php error.

On line 20 (in your code) or line 17 in the above example you are missing a semi colon. :)
echo "Connected made"
should be...
echo "Connected made";
pauls74462
Forum Newbie
Posts: 16
Joined: Tue Jun 10, 2008 8:13 pm

Re: create tables and fields

Post by pauls74462 »

jaoudestudios wrote:You have a php error.

On line 20 (in your code) or line 17 in the above example you are missing a semi colon. :)
echo "Connected made"
should be...
echo "Connected made";

Code: Select all

 
 
$hostname  = "localhost";
$dbusername  = "MyServer_MyUser";
$dbpassword  = "**************";
$dbname  = "MyServer_My_db";
 
 
$con1 = mysql_connect($hostname,$dbusername,$dbpassword);
 
if (!$con1)
  {
  die('Could not connect: ' . mysql_error());
  }
echo "Connected made";
 
 
 mysql_select_db($dbname, $con1);  
$sql = "CREATE TABLE SSDB
 (
   Level varchar(5) NOT NULL,
   QN int(2) NOT NULL,
   Question varchar(50) NOT NULL,
   Answer varchar(50) NOT NULL,
   Wrong1 varchar(50) NOT NULL,
   Wrong2 varchar(50) NOT NULL,
   Wrong3 varchar(50) NOT NULL,
   HINT varchar(50) NOT NULL,
   ImagePath1 varchar(50) NOT NULL,
   ImagePath2 varchar(50) NOT NULL,
 ENGINE=MyISAM DEFAULT CHARSET=utf8 )";
// or die('Could not create: ' . mysql_error());  <This line gave an error so I commented it out.
  
 echo "Tables Created!";
 
 
Now I get the message "table created" but when I go into phpMyAdmin there is no tables.

Any ideal what is wrong?

Paul
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: create tables and fields

Post by Syntac »

You have to execute the query...

Code: Select all

mysql_query($sql, $con1) or die("Query failed. Error: ".mysql_error());
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: create tables and fields

Post by jaoudestudios »

Syntac is right, you are not sending the query to the mysql server.
pauls74462
Forum Newbie
Posts: 16
Joined: Tue Jun 10, 2008 8:13 pm

Re: create tables and fields

Post by pauls74462 »

Syntac wrote:You have to execute the query...

Code: Select all

mysql_query($sql, $con1) or die("Query failed. Error: ".mysql_error());

This is my code now. Where in my code do I place this line?



$hostname = "localhost";
$dbusername = "MyServer_MyUser";
$dbpassword = "**************";
$dbname = "MyServer_My_db";


$con1 = mysql_connect($hostname,$dbusername,$dbpassword);

if (!$con1)
{
die('Could not connect: ' . mysql_error());
}
echo "Connected to db ";



mysql_select_db($dbname, $con1);
$sql = "CREATE TABLE SSDB
(
Level varchar(5) NOT NULL,
QN int(2) NOT NULL,
Question varchar(50) NOT NULL,
Answer varchar(50) NOT NULL,
Wrong1 varchar(50) NOT NULL,
Wrong2 varchar(50) NOT NULL,
Wrong3 varchar(50) NOT NULL,
HINT varchar(50) NOT NULL,
ImagePath1 varchar(50) NOT NULL,
ImagePath2 varchar(50) NOT NULL,
)";
// Execute query
mysql_query($sql, $con1) or die("Query failed. Error: ".mysql_error());

echo "Tables Created!";

// mysql_query($sql,$con1);

mysql_close($con1);

Is something in the wrong place?

Paul
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: create tables and fields

Post by Syntac »

Looks good to me. Give it a try.
Post Reply