Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > PHP Databases
Password Reminder
| FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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;

if(!$result = mysql_query($query, $link_id)) die("Error");
Reply With Quote
  #2 (permalink)  
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

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

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

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

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

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,

Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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

All times are GMT -4. The time now is 11:57 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.