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
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Reply
 
LinkBack Thread Tools Display Modes
Old 01-28-2008, 07:41 PM   #1 (permalink)
The Gregarious
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 563
Thanks: 15
Village Idiot is on a distinguished road
Default Multiple mysql queries

Its been bugging me on how to do multiple mysql queries without a new mysql_query statement being called each time. This essentially calls a new one on every command, but its much cleaner

PHP Code:
$query "INSERT INTO `table` VALUES (bla,bla);
INSERT INTO `another_Table` VALUES (bla,bla);"
;

$arrexplode';'$query );
foreach( 
$arr as $command )
{
    
mysql_query$command );

As long as every command is separated with a semi-colon (which is required in regular mysql), it will execute them all.
__________________
There are two ways to write bug-free code, only the third one works.
Village Idiot is online now  
Reply With Quote
Old 01-28-2008, 07:44 PM   #2 (permalink)
The Addict
Top Contributor Good Samaritan 
 
Join Date: Jan 2008
Location: USA
Posts: 218
Thanks: 16
RobertK is on a distinguished road
Default

I'd run a "rtrim" on every line for cleanliness, but this is essentially what I've done in my database class.
__________________
Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning. - Rich Cook
RobertK is offline  
Reply With Quote
Old 01-28-2008, 08:04 PM   #3 (permalink)
The Contributor
 
buggabill's Avatar
 
Join Date: Jan 2008
Location: Maine, USA
Posts: 92
Thanks: 2
buggabill is on a distinguished road
Default

The only way that I know of is if you can use the mysqli library. Check out mysqli_multi_query.
__________________
-- Bill
"Why is it drug addicts and computer aficionados are both called users?" -Clifford Stoll
buggabill is offline  
Reply With Quote
Old 01-28-2008, 08:13 PM   #4 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 445
Thanks: 49
ReSpawN is on a distinguished road
Default

The best thing you can do is simply (for this example) is to rip a MySQL SQL Dump from phpMyAdmin. It has all the characters you need to escape.

Every sentence that starts with '--' is an comment line, and every sentence that starts with a regular command like INSERT, UPDATE, TRUNCATE and ends with a ;, is a query.

To clarify, rtime removes all the whitespace behind each line. So if you (for human readability) want to use enters, that's fine. No problem there. Even TABs are fine.

For as far as I know, there is no way to perform multiple inserts without using a explode command to get rid of all the ... bullshit so to speak. The lines have to be cleaned first.

I tried my own using the foreach loop ($values as $value) or something like that and it worked. Aside of the last rule where it suddenly failed. But that's a bug in my script.

I'm guessing you want to use this for an installation?

Mark

edit,

Bill beat me to it lol, the mysqli way (as I just found out) is possible as well. You can then even import your file using file_get_contents();
Awesome way as well, I didn't know it was possible, after some research.
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 01-28-2008, 08:32 PM   #5 (permalink)
The Gregarious
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 563
Thanks: 15
Village Idiot is on a distinguished road
Default

Quote:
Originally Posted by ReSpawN View Post
I'm guessing you want to use this for an installation?
.
That is what I built it for, there are over 35 commands taking just over 2 pages. I didn't want to use mysqli, more work then its worth IMO for something like this
__________________
There are two ways to write bug-free code, only the third one works.
Village Idiot is online now  
Reply With Quote
Old 01-28-2008, 08:34 PM   #6 (permalink)
The Addict
Top Contributor Good Samaritan 
 
Join Date: Jan 2008
Location: USA
Posts: 218
Thanks: 16
RobertK is on a distinguished road
Default

It's only a good thing IF you have mysqli, and as we all know servers aren't maintained equally. Mine has no mysqli and no PDO, doubtless other's lack these too--lacking either one, both, or neither. It is a design flaw not to prepare for these possibilities, in my opinion.

I too have used the method, as I said before, without error.

Just a thought, but to split the string by ';' yields preceding whitespace, so the rtrim mention should be an ltrim.
__________________
Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning. - Rich Cook
RobertK is offline  
Reply With Quote
Reply



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


All times are GMT. The time now is 03:42 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0