Page 1 of 1

PHP Executing Differently at the Client Side

Posted: Tue Feb 17, 2004 7:37 pm
by hof
Hello Folks

I have a very strange problem, something I have never encountered before. Some PHP code seems to be executing differently on different users machines, something that seems to totally defy server side code.

Here's what is happening. Lets say I have a table called 'names' with two fields and an auto increment on the ID...

ID | name

Then I have an array, which builds up an MySQL insert statement.

Code: Select all

<?php

$array1[0] = 'peter';
$array1[1] = 'paul';
$array1[2] = 'mary';

$insertStatement = 'INSERT INTO names VALUES ';

foreach ($array1 as $var => $val)
{
    $insertStatement .= '(null, "'. $val .'"), ';
}

echo substr($insertStatement, 0, -2);

mysql_query(substr($insertStatement, 0, -2), $myDatabaseConnection);

echo mysql_insert_id($myDatabaseConnection);

?>
The wierd thing is for 98 out of 100 users this processes fine. But, for 2 of those users when you go echo substr($insertStatement, 0, -2); it returns...

INSERT INTO names VALUE

yet the database insert works. BUT, mysql_insert_id returns 0 even if it was the 100th record to be inserted.

Anyone know whats going on here? This has me completely mystified.

thx in advance

Posted: Tue Feb 17, 2004 8:01 pm
by markl999
See if ...
mysql_query(substr($insertStatement, 0, -2), $myDatabaseConnection) or die(mysql_error()); sheds any light on it.
But, for 2 of those users
What are those users names out of interest?

Posted: Tue Feb 17, 2004 8:30 pm
by hof
It does produce a MySQL error...

"You have an error in your syntax near INSERT INTO names VALUE"

But the damn thing still actually gets inserted, something that is seemingly impossible.
What are those users names out of interest?
Is this really relevant? It is John and Graham, if that is of any consequence!

Cheers

Posted: Wed Feb 18, 2004 5:41 am
by markl999
The names could have been relevant, they might have contained unique in containing certain characters .. but they're not so it rules that out. The only thing i can think of is that the $insertStatement is constantly being appended to , so you're somehow ending up with "INSERT INTO ...blah blah .. INSERT INTO foo bar" etc.. but your pasted code above doesn't make this possible.
Is that the exact code you're using? If not might be worth posting that in case there's a subtle difference.

Posted: Wed Feb 18, 2004 6:04 am
by JayBird
try echoing the $insertStatement before you ececute it to make sure the MySQL statement is constructed how you expect. Post the result back here.

Mark