For Programmers: Free Programming Magazines  


Home > Archive > PHP DB > May 2005 > RE: [PHP-DB] multiple queries, one transaction









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author RE: [PHP-DB] multiple queries, one transaction
Mayo

2005-05-17, 8:56 pm

Thanks, the $orderId = mysql_insert_id($result) was what I was looking
for.

But in the long run, I'm still concerned about how to group several
queries into one transaction. As for example when one wants to make
multiple inserts with one submit.

Ex: User wants to input inventory. He's going to put in 11 new jeans
sizes 28-38

You create a loop inserting the following

itemType:jean
itemDesigner:Antik
itemSize:$i

This should all be one transaction. I would like to do this with
php/mysql.

Thx



-----Original Message-----
From: Jason [mailto:jasonc@lifepowersales.com]
Sent: Saturday, May 17, 2003 1:30 PM
To: 'mayo'
Subject: RE: [PHP-DB] multiple queries, one transaction

You could always use $orderId = mysql_insert_id($result) to pull the
autoinc
id that was created from your insert statement...

But I think your question revolves more around "how do I do a query"
then
"how do I pull the id back"
So to answer what I think your asking...
I write my queries like this:

$query = "INSERT into orders(
orderData)
values(
'$orderData')";
$result = mysql_query($query);
$orderId= mysql_insert_id($result);

Hope that helps.

-----Original Message-----
From: mayo [mailto:mayo@nycinteractive.com]
Sent: Tuesday, May 17, 2005 8:27 AM
To: php-db@lists.php.net
Subject: [PHP-DB] multiple queries, one transaction

I would like to get the itemID number (autoincrement) of the last
insert.

(Insert order, get last orderID number and use it elsewhere.)

I'm having trouble understanding how to do a transaction in mysql/php

Code below:



$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die
('Error connecting to mysql');

$dbname = 'mail';

mysql_query("BEGIN"); // starts the transaction

mysql_query($query) or die('Error, insert query failed');
mysql_query($query2) or die('Error, select query failed');


$query = "INSERT INTO orders (orderDate) VALUES ('2005-05-17')";
$query = "SELECT max ordered FROM orders";

mysql_query($query) or die('Error, insert query failed');
mysql_query($query2) or die('Error, update query failed');


mysql_query("COMMIT"); // ends the transaction

mysql_close($conn);
?>

thx

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com