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')
Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.