创建新表时出现MySQL语法错误

时间:2012-09-10 14:21:12

标签: php mysql

我在下面的代码中对查询没有任何问题。但第二个代码是抛出错误

  

您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在第1行的''附近使用正确的语法

工作代码:

<?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();
?>

生成代码时出错;

<?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)."<br>";

echo "Last Field :".$lastField."<br>";

$result ="CREATE TABLE $tableName(";

for($i = 0; $i < 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."<br>".$i."<br>";} else {Echo "Some Problem!";}

mysql_connect("localhost", "","") or die(mysql_error());
mysql_select_db("") or die(mysql_error());

mysql_query($result);

echo mysql_error();

?>

我在第一个代码中使用了此处的$result输出,它运行得非常好!我在第二个代码中哪里出错了?

$ result echo给出了

  

CREATE TABLE google_csv(ÿþNamevarchar(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),位置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_ 类型int(11),Phone_1 _Value int(11),Phone_2_ 类型int(11),Phone_2 _Value int( 11),Phone_3_ 类型int(11),Phone_3 _Value int(11),Address_1_ 类型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))

看起来一切都很好!但那......“

1 个答案:

答案 0 :(得分:0)

这是什么:

  

YTH

$result ="CREATE TABLE google_csv(ÿþName ....")

           -----------------------^

ÿþ是特殊字符(0xFE,0xFF)。我不认为OP想要colum名称,如果是这样,它应该被

括起来
`ÿþName`

http://en.wikipedia.org/wiki/ISO_8859-1