0

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

您的 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 回显给出

创建表 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) ,电话_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___值 int(11))

看起来这里一切都很好!但是之后......

4

1 回答 1

0

这是什么 :

ÿþ

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

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

ÿþ 是特殊字符(0xFE、0xFF)。我不认为 OP 想要它们在列名中,如果是这样,它应该由

`ÿþName`

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

于 2012-09-10T14:26:00.950 回答