MYSQL IN A PHP LOOP

MYSQL IN A PHP LOOP

am 09.11.2005 19:12:11 von Malcolm JC Clark

I have a php function that creates the following SQL below with this
statement in the loop:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$myQuery .= " UPDATE page SET
page_order_id = \"$page_order_id\"
WHERE page_id = \"$page_id\";
";
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



$myQuery looks finally like this:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~
UPDATE page SET page_order_id = "3" WHERE page_id = "1"; UPDATE page SET
page_order_id = "5" WHERE page_id = "2"; UPDATE page SET page_order_id = "2"
WHERE page_id = "3"; UPDATE page SET page_order_id = "5" WHERE page_id =
"4"; UPDATE page SET page_order_id = "6" WHERE page_id = "5"; UPDATE page
SET page_order_id = "2" WHERE page_id = "13";
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


I get this (custom)error when the query is sent to mysql_query()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Err: You have an error in your SQL syntax near '; UPDATE page SET
page_order_id = "5" WHERE page_id = "2"' at line 4
Err No.:1064
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



Stuck into PhpMyAdmin does not produce an error, and I know that there are
functions behind PhpMyAdmin that format user's SQL statements, and I wish I
knew how!!

It is maybe something to do with the semi-colons? Or that I can not normally
send several UPDATE statements in one query???


PLease help.


Malc







___ __ _ _ ____ ___
/ __) /__\ ( \/\/ )( _ \ / __)
\__ \ /(__)\ ) ( )(_) )\__ \
(___/(__)(__)(__/\__)(____/ (___/

Strategically Applied Web Development Systems
Contact: Malcolm JC Clark
Tel: 0161 408 2883
Mobile: 07779 034 868

This e-mail may contain confidential and/or privileged information. If you
are not the intended recipient (or have received this e-mail in error)
please notify the sender immediately and destroy this e-mail. Any
unauthorised copying, disclosure or distribution of the material in this
e-mail is strictly forbidden.

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: MYSQL IN A PHP LOOP

am 09.11.2005 19:57:31 von Bastien Koert

You can't stack queries this way. PHPMyAdmin parses out the queries based on
the ';'. You will need to run the query after each one is created.

Bastien


>From: "Malcolm JC Clark"
>Reply-To:
>To:
>Subject: [PHP-DB] MYSQL IN A PHP LOOP
>Date: Wed, 9 Nov 2005 18:12:11 -0000
>
>I have a php function that creates the following SQL below with this
>statement in the loop:
>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>$myQuery .= " UPDATE page SET
> page_order_id = \"$page_order_id\"
> WHERE page_id = \"$page_id\";
> ";
>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
>
>
>$myQuery looks finally like this:
>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~
>UPDATE page SET page_order_id = "3" WHERE page_id = "1"; UPDATE page SET
>page_order_id = "5" WHERE page_id = "2"; UPDATE page SET page_order_id =
>"2"
>WHERE page_id = "3"; UPDATE page SET page_order_id = "5" WHERE page_id =
>"4"; UPDATE page SET page_order_id = "6" WHERE page_id = "5"; UPDATE page
>SET page_order_id = "2" WHERE page_id = "13";
>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
>
>I get this (custom)error when the query is sent to mysql_query()
>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>Err: You have an error in your SQL syntax near '; UPDATE page SET
>page_order_id = "5" WHERE page_id = "2"' at line 4
>Err No.:1064
>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
>
>
>Stuck into PhpMyAdmin does not produce an error, and I know that there are
>functions behind PhpMyAdmin that format user's SQL statements, and I wish I
>knew how!!
>
>It is maybe something to do with the semi-colons? Or that I can not
>normally
>send several UPDATE statements in one query???
>
>
>PLease help.
>
>
>Malc
>
>
>
>
>
>
>
> ___ __ _ _ ____ ___
>/ __) /__\ ( \/\/ )( _ \ / __)
>\__ \ /(__)\ ) ( )(_) )\__ \
>(___/(__)(__)(__/\__)(____/ (___/
>
>Strategically Applied Web Development Systems
>Contact: Malcolm JC Clark
>Tel: 0161 408 2883
>Mobile: 07779 034 868
>
>This e-mail may contain confidential and/or privileged information. If you
>are not the intended recipient (or have received this e-mail in error)
>please notify the sender immediately and destroy this e-mail. Any
>unauthorised copying, disclosure or distribution of the material in this
>e-mail is strictly forbidden.
>
>--
>PHP Database Mailing List (http://www.php.net/)
>To unsubscribe, visit: http://www.php.net/unsub.php
>

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: MYSQL IN A PHP LOOP

am 11.11.2005 04:45:08 von Arie Nugraha

------=_Part_18719_6065933.1131680708086
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Multiple query can only be done in PHP 5 with MySQLI extension.

In PHP 4 maybe you can use an array to store the query and execute each
query with foreach :

$query[] =3D "UPDATE bla.. bla..";
$query[] =3D "UPDATE bla.. bla..";
$query[] =3D "UPDATE bla.. bla..";

foreach ($query as $eachquery){
mysql_query($eachquery);
}

?>

------=_Part_18719_6065933.1131680708086--