PHP MYSQL issues

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Flycow
Forum Newbie
Posts: 9
Joined: Thu Jul 08, 2010 8:04 am

PHP MYSQL issues

Post 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);
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: PHP MYSQL issues

Post 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.
Flycow
Forum Newbie
Posts: 9
Joined: Thu Jul 08, 2010 8:04 am

Re: PHP MYSQL issues

Post by Flycow »

Excellent thanks for the help - Made me realise that MySQL wouldn't accept the header with spaces!
Post Reply