Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP Databases
| Search | Today's Posts | Mark Forums Read
PHP Databases Using PHP in conjunction with databases. PHP questions not specific to databases should be directed to one of the other PHP forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 5th, 2003, 07:41 PM
Registered User
 
Join Date: Jul 2003
Location: , , Canada.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default executing multiple statements

I have been having trouble executing multiple sql statements. here is an example:

$query = "INSERT INTO $tbl_name ( SKU, prod_title, retail_price, best_seller, new_item) values ( '$sku_element', '$title_element', '$price_element', '$best_element', '$new_item_element');";

$query .= "INSERT INTO $tbl_name (SKU, instrument, instrumentCategory) values ('$sku_element', '$inst_element', '$instCat_element');";

echo $query;

// EXECUT QUERY
if(!$result = mysql_query($query, $link_id)) die("Error");
 
Old July 7th, 2003, 02:19 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You can't execute multiple multiple queries with a call to mysql_query(). You need to break it up into multiple queries.

Code:
$query = "INSERT INTO $tbl_name
                 (SKU, prod_title, retail_price, best_seller, new_item)
          VALUES
                 ('$sku_element', '$title_element', '$price_element',
                  '$best_element', '$new_item_element')";

// EXECUTE FIRST QUERY
echo "First query: $query <br/>\n";
$result = mysql_query($query, $link_id));

$query = "INSERT INTO $tbl_name
                 (SKU, instrument, instrumentCategory)
          VALUES
                 ('$sku_element', '$inst_element', '$instCat_element');";

// EXECUTE SECOND QUERY:
echo "Second query: $query <br/>\n";
$result = mysql_query($query, $link_id));
Of course, this is where MySQL's lack of transactions really hurts developers who need to perform multiple queries as part of a single transaction.

If the first query succeeds but the second fails, you need to undo the first query. With a transactions-enabled database, this is easy; you just wrap both queries into a transaction.

With MySQL, though, transactions aren't supported using the default table type. Using the Inno DB table type slows things down a bit, but that's the price you pay when you need transaction support.

The other option is to manually mimic transactions in the code. If the first query succeeds and the second fails, run another query that deletes from $tbl_name where SKU = $sku_element, prod_title = $title_element, etc...

Of course, this isn't fool proof -- this rollback query might fail, too.


Take care,

Nik
http://www.bigaction.org/




Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple LIKE statements melkin MySQL 3 December 17th, 2008 03:38 PM
if statements for multiple date checks melkin SQL Language 0 December 17th, 2007 09:53 AM
Multiple IIF Statements rohit_ghosh Access 2 May 7th, 2007 03:05 PM
Multiple IIF statements Corey Access 1 November 16th, 2006 05:25 PM
Multiple Insert Statements tp194 Classic ASP Databases 8 June 11th, 2003 01:26 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.