0

I have no issues with the query in the below code. But the second code is throwing the error

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

Working code :

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

Error producing code;

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

?>

I am using the $result output from here in the first code and it works perfectly fine! Where am I going wrong in the second code?

Please help. Have tried other forums. Nobody could help. Hope to find the solution here.

2
Contributors
3
Replies
6
Views
5 Years
Discussion Span
Last Post by smantscheff
0

The $result echo is

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))

0

Is this the error-producing query? I don't think so. I asked for the content of $result in you 2nd code sample.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.