Page 1 of 1

PHP MYSQL issues

Posted: Fri Jul 09, 2010 4:07 pm
by Flycow
While this doesnt produce any errors it doesnt create the table.

Any ideas.

Code: Select all

$con = mysql_connect("localhost",$dbuser,$dbpassword);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db($dbname, $con);

$file = fopen($csvfile,"r");

$data = fgetcsv($file,1000,",");

$sql = 'CREATE TABLE ' . $table . ' (';
$c = count($data) - 1;
for ($i=0; $i<=$c; $i++)
  {
if ($i==0)
  $sql .= $data[$i] . ' VARCHAR(' . $length . ') NOT NULL, ';
else
  $sql .= $data[$i] . ' VARCHAR(' . $length . '), ';
  }
$sql .= 'PRIMARY KEY (' . $data[0] . '))';
echo $sql;

mysql_query($sql, $con);
mysql_close($con);

Re: PHP MYSQL issues

Posted: Fri Jul 09, 2010 6:45 pm
by JakeJ
Try this:

mysql_query($sql, $con) or die(mysql_error());

See what it returns and that will give you a better idea of what's going wrong.

For starters unless you have $table defined somewhere above the code you pasted in, it's going to fail there since $table would not otherwise contain anything.

But mostly, find out what error is being generated and go from there. Post back the results if you can't get it figured out.

Re: PHP MYSQL issues

Posted: Sat Jul 10, 2010 2:19 am
by Flycow
Excellent thanks for the help - Made me realise that MySQL wouldn't accept the header with spaces!