TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 07-16-2009, 11:30 AM   #1 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default Handling a large number of inserts on a mysql table

I need to cater for a situation where i want to process multiple inserts onto a mysql table. There are 4 columns on the table and 2 of the columns will always take the same data for the insert eg.

column1, column2, column3, column4
joebloggs,london,1234,ABC
joebloggs,london,1564,Akfr
joebloggs,london,19354,kjf
joebloggs,london,1354,AHC

Is there a mysql command i can use to do the insert as one package of work or do I need to loop in the php code until all the inserts are done?

If the latter, i'd need to checkpoint and commit. is this easy to do?
captainmerton is offline  
Reply With Quote
Old 07-16-2009, 01:43 PM   #2 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Smile

You can loop to concatenate the query string and then query at the end, that way there is not a lot of sequential queries, but just one.
PHP Code:
$query 'INSERT INTO thetable(column1, column2, column3, column4) ';
//$assuming the data is comming from a db query.
while ($row mysql_fetch_assoc($rs)) {
    
$query .= 'VALUES('.$row['name'].', '.$row['city'].', '.$row['special_number'].', '.$row['code'].') ';
}
$result mysql_query($query) or die(mysql_error()); 
tony is offline  
Reply With Quote
The Following User Says Thank You to tony For This Useful Post:
captainmerton (07-16-2009)
Old 07-16-2009, 02:01 PM   #3 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

Thats exactly what i was looking for. Cheers tony.
captainmerton is offline  
Reply With Quote
Old 07-16-2009, 02:02 PM   #4 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Making it into one query will make it run far faster. It will work for methods like this, but PHP does not allow more than one command per mysql_query. I consider this one of PHPs weak points since as you get more advanced with SQL, multiple queries become the best way to do things.
__________________

Village Idiot is offline  
Reply With Quote
Old 07-16-2009, 02:25 PM   #5 (permalink)
The Acquainted
 
JaoudeStudios's Avatar
 
Join Date: Jul 2009
Location: Surrey
Posts: 105
Thanks: 1
JaoudeStudios is on a distinguished road
Default

Quote:
Originally Posted by Village Idiot View Post
...but PHP does not allow more than one command per mysql_query. I consider this one of PHPs weak points since as you get more advanced with SQL...
You should not be using the MySQL library in PHP anymore, but the MySQLi which has been out for a while. PHP.net has deprecated MySQL api and insists on using the newer version MySQLi (stands for MySQL improved) - it has great benefits in speed, security etc...

MySQLi also allows multiple queries and much more. However note there is a downside to multiple queries, it is more vulnerable! So make sure you protect your queries well!
JaoudeStudios is offline  
Reply With Quote
Old 07-16-2009, 02:55 PM   #6 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

I haven't used PHP myself for about seven months, so I guess I'm out of date. I'll keep that in mind, thanks.
__________________

Village Idiot is offline  
Reply With Quote
Old 07-16-2009, 03:09 PM   #7 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

I have meant to dive into mysqli, but I haven't used php for a while. for some reason the recent projects I am working on have a windows server with no php install, so only .NET for now.
tony is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
feedback on my class please frostyboy33 Advanced PHP Programming 7 10-22-2012 09:12 AM
Databse structure Village Idiot TalkPHP Developer Team 3 01-16-2009 10:31 PM
This project has begun! Village Idiot TalkPHP Developer Team 40 01-01-2009 04:29 AM
Securing your MySQL Queries with Sprintf Wildhoney General 26 03-18-2008 06:52 PM
Notepage like application to open large MySQL files Wildhoney General 6 12-07-2007 02:18 PM


All times are GMT. The time now is 09:07 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design