Page 1 of 1

create tables and fields

Posted: Tue Dec 02, 2008 3:48 pm
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!";
 
 
?>
 

Re: create tables and fields

Posted: Wed Dec 03, 2008 2:56 am
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";

Re: create tables and fields

Posted: Wed Dec 03, 2008 3:03 pm
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

Re: create tables and fields

Posted: Wed Dec 03, 2008 4:57 pm
by Syntac
You have to execute the query...

Code: Select all

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

Re: create tables and fields

Posted: Thu Dec 04, 2008 1:19 am
by jaoudestudios
Syntac is right, you are not sending the query to the mysql server.

Re: create tables and fields

Posted: Sat Dec 06, 2008 9:52 am
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

Re: create tables and fields

Posted: Sat Dec 06, 2008 3:23 pm
by Syntac
Looks good to me. Give it a try.