Jump to content

Archived

This topic is now archived and is closed to further replies.

snakez

How to merge Multiple queries into a single variable

Recommended Posts

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')

Share this post


Link to post
Share on other sites
If you mean run one query that does all 3, then no, not really... one insert per table per statement.

Share this post


Link to post
Share on other sites
fenway is correct, however, you [b]could[/b] 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:
[code]
<?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);
}
?>
[/code]

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.