PHP Executing Differently at the Client Side

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hof
Forum Newbie
Posts: 2
Joined: Tue Feb 17, 2004 7:37 pm

PHP Executing Differently at the Client Side

Post 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
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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?
hof
Forum Newbie
Posts: 2
Joined: Tue Feb 17, 2004 7:37 pm

Post 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
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
Post Reply