PHP MySQL error

Query string returns an erroe

Page 1 of 1

6 Replies - 963 Views - Last Post: 26 October 2010 - 01:57 AM Rate Topic: -----

#1 Nkosinathi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-October 10

PHP MySQL error

Posted 26 October 2010 - 12:30 AM

Hi
Thanks in advance for helping me. I have this mySQL query that fails i have no idea why i. Below is the code I left some space below for readability

$lines=file("somefile5640.001");
$headerTable=explodeX(Array(",","\n"),$lines[0]);
$table ="HeaderRecord";

WriteArrayToHeaderDatabase($headerTable,$table);

function WriteArrayToHeaderDatabase($headerTable,$table)//Pass which table am i writing to
{

//I do the connecting to database here which is fine the problem is below

$query = "INSERT INTO ".$table."(FileIdentifier, Version, ReceiverIdentifier, Date, Time, FileGenerationNumber) VALUES";

for($i = 0, $c = count($headerTable); $i < $c; $i += 6) {
// Add the next batch of values to the query string
$query.= "('".$headerTable[$i]."','".$headerTable[$i+1]."','".$headerTable[$i+2]."','".$headerTable[$i+3]."','".$headerTable[$i+4]."','".$headerTable[$i+5]."')";
//$query .= "(".$headerTable[$i].",".$headerTable[$i+1].",".$headerTable[$i+2].",".$headerTable[$i+3].",".$headerTable[$i+4].",".$headerTable[$i+5].")";
echo "Query is failing";

// Add a comma is this is not the last batch
if($i + 1 < $c) {
//$query .= "";
$query .=",";
}

echo $query; 
$result = mysql_query($query);

if (!$result) {
die('Query failed: ' . mysql_error());
} 

}
return true; 



// When i echo the query and run the code i get what shown below

Query is failingINSERT INTO HeaderRecord(FileIdentifier, Version, ReceiverIdentifier, Date, Time, FileGenerationNumber) VALUES('SOF','1','1500','19970519','112904','1 '),Query failed: 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

This post has been edited by no2pencil: 26 October 2010 - 12:31 AM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: PHP MySQL error

#2 no2pencil  Icon User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6585
  • View blog
  • Posts: 30,745
  • Joined: 10-May 07

Re: PHP MySQL error

Posted 26 October 2010 - 12:36 AM

Quote

INSERT INTO HeaderRecord(FileIdentifier, Version, ReceiverIdentifier, Date, Time, FileGenerationNumber) VALUES('SOF','1','1500','19970519','112904','1 '),


I can't tell if your comma is part of the error message, or because of this : $query .=",";
Was This Post Helpful? 0
  • +
  • -

#3 Nkosinathi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-October 10

Re: PHP MySQL error

Posted 26 October 2010 - 01:03 AM

When i comment out (as below)that

// Add a comma is this is not the last batch
//if($i + 1 < $c) {
//$query .= "";
//$query .=",";
//}

i get the following error and the record get written and why is the query being repeated?



Query is failingINSERT INTO HeaderRecord(FileIdentifier, Version, ReceiverIdentifier, Date, Time, FileGenerationNumber) VALUES('SOF','1','1500','19970519','112904','1 ')Query is failingINSERT INTO HeaderRecord(FileIdentifier, Version, ReceiverIdentifier, Date, Time, FileGenerationNumber) VALUES('SOF','1','1500','19970519','112904','1 ')('','','','','','')Query failed: 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
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6585
  • View blog
  • Posts: 30,745
  • Joined: 10-May 07

Re: PHP MySQL error

Posted 26 October 2010 - 01:13 AM

It's being output twice because you output the variable $query on the very 1st line after your comment, & then the sql fails, outputting the attempted query.

This can be proven by editing the following :

	echo "<p>SQL : ".$query."</p>";
	$result = mysql_query($query);
	 
	if (!$result) {
	die('Query failed: ' . mysql_error());
	} 

Was This Post Helpful? 0
  • +
  • -

#5 Nkosinathi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-October 10

Re: PHP MySQL error

Posted 26 October 2010 - 01:52 AM

OK,think I follow you, but still i used your code the error still remains

Query is failing
SQL : INSERT INTO HeaderRecord(FileIdentifier, Version, ReceiverIdentifier, Date, Time, FileGenerationNumber) VALUES('SOF','1','1500','19970519','112904','1 ')

Query is failing
SQL : INSERT INTO HeaderRecord(FileIdentifier, Version, ReceiverIdentifier, Date, Time, FileGenerationNumber) VALUES('SOF','1','1500','19970519','112904','1 ')('','','','','','')

Query failed: 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
Was This Post Helpful? 0
  • +
  • -

#6 no2pencil  Icon User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6585
  • View blog
  • Posts: 30,745
  • Joined: 10-May 07

Re: PHP MySQL error

Posted 26 October 2010 - 01:53 AM

I didn't say it would fix the error. I just said it would better explain why you are getting two errors.
Was This Post Helpful? 0
  • +
  • -

#7 Nkosinathi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-October 10

Re: PHP MySQL error

Posted 26 October 2010 - 01:57 AM

OK
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1