Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to pass array into another array 1

Status
Not open for further replies.

matthewgl

IS-IT--Management
Jun 19, 2008
14
US
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";

//select a database to work with
$selected = mysql_select_db("$dbname",$dbhandle)
or die("Could not select $dbname");

//execute the SQL query and return records
$result = mysql_query("SELECT orders_id FROM orders WHERE
customers_id='1'");

Now this gets 2-3 rows, or more specifcly 2-3 cells from orders_id.
Id like to pass those 2-3 cells into another mysql connection, because it needs to take those 2-3 cells and do a query like so(lets say i pass the 2-3 cells to $cells):


//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";

//select a database to work with
$selected = mysql_select_db("$dbname",$dbhandle)
or die("Could not select $dbname");

//execute the SQL query and return records
$result = mysql_query("SELECT * FROM orders_products WHERE
orders_id='$cells'");

//fetch tha data from the database
while ($row = mysql_fetch_array($result)) {
echo "Product Name: ".$row{'products_name'};
}

Im not sure how to set the 2-3 cells to a variable since its an array, and i dont know how to recieve it into another mysql database connection.

Any help please?
thanks
 
You'll need to put it into a re-usable variable first. otherwise the moment you initiate the next connection you'll loose the contents of your variables since thy are called the same in both queries.

So:

Code:
$orders=array();
while($cell=mysql_fetch_array($result){
$orders[]=$cell['orders_id'];
}

Now you can use the orders variable for your next query.

In your next query you want to select records that match any of the order_id's you extracted previously?

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
awesome,

"In your next query you want to select records that match any of the order_id's you extracted previously?"

correct!

Not sure how to do that, something like this i suppose:
//execute the SQL query and return records
$result = mysql_query("SELECT * FROM orders_products WHERE
orders_id='$orders'");

that doesnt work tho because $orders is an array not just a single variable i imagine...not sure how i would do that?
 
$result = mysql_query("SELECT * FROM orders_products WHERE
orders_id='$orders[0]'");

i can do that, but not sure how to get it to do [1][2][3][4] etc
 
the two connections and databases look identical. If this is the case then you do not need to re connect or re select. And in fact you could probably do the whole thing in one query.
 
Loop through it.

Code:
$query="SELECT * FROM orders_products WHERE ";
$i=0;
foreach($orders as $myorder){
$query.="order_id=$myorder";
$i++;
if($i<count($orders)){
$query.=" OR ";
}
}
[GREEN]\\You can now use your constructed query.[/GREEN]
$result = mysql_query($query);


----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
jpadie:

I run the first:
$result = mysql_query("SELECT ORDERS_ID FROM orders WHERE customers_id='1'");

That returns orders_ids (1,2,3,etc)

Then i need to run another query to find the data from table Orders_products using each of the orders_ids (1,2,3,etc):
$result = mysql_query("SELECT * FROM orders_products WHERE orders_id=$orders[1,2,3,etc]");

Im not sure how to get all the data for $orders[1,2,3,etc]
The number of orders can change, but it should not go over 1-4 orders...so i _could_ just run 4 queries, orders[0], orders[1], etc....but it would be nice to do it in as little as i can...

thanks! hope i made sense
 
I agree with jpadie that this would have been easier done in a single query using joins. But my above example should work anyway.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
and whether or not you do in one or two queries as per vacunita's code, you do not/should not reinstantiate the database server connection and database selection each time you perform a query.
 
vacunita: thanks that worked perfectly!

jpadie: thanks for the tip, i merged it into one database connection

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top