Jump to content

How to merge Multiple queries into a single variable


snakez

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

Archived

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

×
×
  • Create New...

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.