Jump to content


Photo

How to merge Multiple queries into a single variable


  • Please log in to reply
4 replies to this topic

#1 snakez

snakez
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 06 September 2006 - 07:55 AM

Ey im just a newbie to php/mysql... i just like to ask a question regarding the sample queries below if i can assign/store a single (PHP)variable to all of it and how to do it.


Insert Into Mytable1(fname,lname) values('nick','smith')
Insert Into Mytable2(ProdID,ProdName) values('01','PC')
Insert Into Mytable3(ORno,Date) values('01','01/01/01')


#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 06 September 2006 - 01:36 PM

If you mean run one query that does all 3, then no, not really... one insert per table per statement.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 06 September 2006 - 01:54 PM

fenway is correct, however, you could possibly run all 3 queries from one mysql_query() call if that's what you're after. it's not advisable since you have no individual error tracking, but since it doesn't hurt to know options, here's an example:
<?php
$code = "Insert Into Mytable1(fname,lname) values('nick','smith');
Insert Into Mytable2(ProdID,ProdName) values('01','PC');
Insert Into Mytable3(ORno,Date) values('01','01/01/01');";
mysql_query($code);

// a better option would be to store all the insert queries in one array
// and loop through the array:
$queries = array(
  "Insert Into Mytable1(fname,lname) values('nick','smith')",
  "Insert Into Mytable2(ProdID,ProdName) values('01','PC')",
  "Insert Into Mytable3(ORno,Date) values('01','01/01/01')"
);

foreach ($queries as $q) {
  mysql_query($q);
}
?>

with the second method, it's much more usable since you can break out of the loop with any given error and figure out what happened.

hope this helps
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#4 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 06 September 2006 - 02:25 PM

obsidian is correct as well... but I would refrain for "faking" multiple queries for a single call.  In fact, I'm surprised that mysql_query() even lets you do this.  It's much, much better to set up the array.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#5 snakez

snakez
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 07 September 2006 - 12:42 AM

Woohh... that was great guyz, thank you so much for the help...  :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users