title: MySQL syntax error while creating a new table
body: <p>I have no issues with the query in the below code. But the second code is throwing the error</p>
  
  <blockquote>
    <p>You 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 1</p>
  </blockquote>
  
  <p>Working code :</p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  mysql_connect("localhost", "","") or die(mysql_error());
  mysql_select_db("") or die(mysql_error());
  
  $result ="  CREATE TABLE google_csv(
      ÿþName varchar(80),
      Given_Name varchar(80),
      Additional_Name varchar(80),
      Family_Name varchar(80),
      Yomi_Name varchar(80),
      Given_Name_Yomi varchar(80),
      Additional_Name_Yomi varchar(80),
      Family_Name_Yomi varchar(80),
      Name_Prefix varchar(80),
      Name_Suffix varchar(80),
      Initials varchar(80),
      Nickname varchar(80),
      Short_Name varchar(80),
      Maiden_Name varchar(80),
      Birthday varchar(80),
      Gender varchar(80),
      Location varchar(80),
      Billing_Information varchar(80),
      Directory_Server varchar(80),
      Mileage varchar(80),
      Occupation varchar(80),
      Hobby varchar(80),
      Sensitivity varchar(80),
      Priority varchar(80),
      Subject varchar(80),
      Notes varchar(80),
      Group_Membership varchar(80),
      E_mail_1___Type int(11),
      E_mail_1___Value int(11),
      E_mail_2___Type int(11),
      E_mail_2___Value int(11),
      Phone_1___Type int(11),
      Phone_1___Value int(11),
      Phone_2___Type int(11),
      Phone_2___Value int(11),
      Phone_3___Type int(11),
      Phone_3___Value int(11),
      Address_1___Type int(11),
      Address_1___Formatted int(11),
      Address_1___Street int(11),
      Address_1___City int(11),
      Address_1___PO_Box int(11),
      Address_1___Region int(11),
      Address_1___Postal_Code int(11),
      Address_1___Country int(11),
      Address_1___Extended_Address int(11),
      Website_1___Type int(11),
      Website_1___Value int(11)
  )";
  
  echo $result;
  
  mysql_query($result);
  echo mysql_error();
  ?&gt;
  </code></pre>
  
  <p>Error producing code;</p>
  
  <pre><code>&lt;?php
  
  // GENERATE TABLE FROM FIRST LINE OF CSV FILE
  
  $inputFile = 'google.csv';
  $tableName = 'google_csv';
  
  $fh = fopen($inputFile, 'r');
  $contents = fread($fh, 5120); // 5KB
  fclose($fh);
  
  $fileLines = explode("\n", $contents);
  
  $fieldList = explode(',', $fileLines[0]);
  
  $lastField=count($fieldList)-1;
  
  echo "Total Fields :".count($fieldList)."&lt;br&gt;";
  
  echo "Last Field :".$lastField."&lt;br&gt;";
  
  $result ="CREATE TABLE $tableName(";
  
  for($i = 0; $i &lt; count($fieldList); $i++)
  {
      if(preg_match('/[1-9]/', $fieldList[$i]))
      {
  
      if ($i==$lastField)
      {$result .=str_replace(array(" ",'-'),"_",$fieldList[$i]). ' int(11)';}
      else
      {$result .=str_replace(array(" ",'-'),"_",$fieldList[$i]). ' int(11),';}
  
      }
      else
      {
  
      if($i==$lastField)
  
      {$result .=str_replace(array(" ",'-'),"_",$fieldList[$i]). ' varchar(80)';}
      else
      {$result .=str_replace(array(" ",'-'),"_",$fieldList[$i]). ' varchar(80),';}
  
      }
  }
  
  $result .=")";
  
  if ($result) {echo $result."&lt;br&gt;".$i."&lt;br&gt;";} else {Echo "Some Problem!";}
  
  mysql_connect("localhost", "","") or die(mysql_error());
  mysql_select_db("") or die(mysql_error());
  
  mysql_query($result);
  
  echo mysql_error();
  
  ?&gt;
  </code></pre>
  
  <p>I am using the <code>$result</code> output from here in the first code and it works perfectly fine! Where am I going wrong in the second code?</p>
  
  <p>The $result echo  gives</p>
  
  <blockquote>
    <p>CREATE TABLE google_csv(ÿþName varchar(80),Given_Name varchar(80),Additional_Name varchar(80),Family_Name varchar(80),Yomi_Name varchar(80),Given_Name_Yomi varchar(80),Additional_Name_Yomi varchar(80),Family_Name_Yomi varchar(80),Name_Prefix varchar(80),Name_Suffix varchar(80),Initials varchar(80),Nickname varchar(80),Short_Name varchar(80),Maiden_Name varchar(80),Birthday varchar(80),Gender varchar(80),Location varchar(80),Billing_Information varchar(80),Directory_Server varchar(80),Mileage varchar(80),Occupation varchar(80),Hobby varchar(80),Sensitivity varchar(80),Priority varchar(80),Subject varchar(80),Notes varchar(80),Group_Membership varchar(80),E_mail_1_<strong>Type int(11),E_mail_1</strong>_Value int(11),E_mail_2_<strong>Type int(11),E_mail_2</strong>_Value int(11),Phone_1_<strong>Type int(11),Phone_1</strong>_Value int(11),Phone_2_<strong>Type int(11),Phone_2</strong>_Value int(11),Phone_3_<strong>Type int(11),Phone_3</strong>_Value int(11),Address_1_<strong>Type int(11),Address_1</strong>_Formatted int(11),Address_1_<strong>Street int(11),Address_1</strong>_City int(11),Address_1_<strong>PO_Box int(11),Address_1</strong>_Region int(11),Address_1_<strong>Postal_Code int(11),Address_1</strong>_Country int(11),Address_1_<strong>Extended_Address int(11),Website_1</strong>_Type int(11),Website_1___Value int(11))</p>
  </blockquote>
  
  <p>Looks like all is fine here ! But then......</p>