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?

3
Contributors
3
Replies
4
Views
4 Years
Discussion Span
Last Post by broj1
2

Echo the problematic query that the second code generates and post it. Also enclose field names within backticks since there might be a possibility that the generated field name is a mysql keyword.

-1

i need help with the codes of the calculator
how to code % and the squareroot

2

@estinah
Please start a new thread for your question. Post the code there you have so far.

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.