473,396 Members | 2,057 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Nested SQL Queries???

40
Hi everyone,

I'm very new to MySQL, so please bear with me.

I have a shopping cart table called 'tbl_cart', and I need to display it's contents on my View Cart page. This is no problem. However, in that table I'm storing the product ID... but I'd like to display the product name. The name is contained in a different table called 'inventory'.

I'm trying to do a secondary SQL query on the inventory table so I can pull out the name. This should be easy since it's indexed by the product ID. Here's my code:

[PHP]<?php
// current session id
$sid = session_id();

$sql = "SELECT *
FROM tbl_cart
WHERE ct_session_id = '$sid'";
$result = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($result) == 0) {
echo "YOUR SHOPPING CART IS EMPTY";
} else {
$product_id = $row['pd_id'];
while($row = mysql_fetch_array($result)) {
$sql = "SELECT *
FROM inventory
WHERE id = '$product_id'";
$result2 = mysql_query($sql) or die(mysql_error());
$row_inv = msql_fetch_array($result2);

echo $row_inv['name']."<br />";
echo "Product ID#: ".$row['pd_id']. " - - - Quantity: ".$row['ct_qty']."<br />";

}
}
?>[/PHP]

Since I was in the middle of a WHILE loop, I didn't want to reset the $result or $row variables, so I used $result2 and $row2. Apparently I can't do this, because I get a Call to undefined function: msql_fetch_array() error on the line where I'm trying to fetch_array($result2).

Am I going about this the wrong way? I'm sure I am. Any advice on the easy and correct way to do this would be appreciated. Thanks!
Nov 19 '07 #1
3 1905
vozzek
40
Nevermind... I learned all about JOINS last night. :)
Nov 20 '07 #2
mwasif
802 Expert 512MB
Good work vozzek. Would you like to share the solution so that others can benefit from it?
Nov 20 '07 #3
vozzek
40
Good work vozzek. Would you like to share the solution so that others can benefit from it?
Sure thing! Here's the code I used:

Expand|Select|Wrap|Line Numbers
  1. <?php 
  2.                $sql = "SELECT *
  3.            FROM tbl_cart, inventory
  4.            WHERE tbl_cart.pd_id = inventory.id
  5.            AND tbl_cart.ct_session_id = '$sid'
  6.            ";
  7. ?>
Worked great. :)
Nov 20 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Jeff Lanfield | last post by:
First of all, I apologize if coalescing is not the right term to describe my problem. I have a tree where each node has the same set of attributes (is the same entity) but child nodes should...
5
by: ahokdac-sql | last post by:
Hi, I'm adapting access queries to sql server and I have difficulties with the following pattern : query1 : SELECT * FROM Query2 WHERE A=@param1 query 2: SELECT * FROM Table2 WHERE B=@param2 ...
3
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I...
2
by: Rashrashetta | last post by:
Dear all, I need to know how to create nested query in MS access ?? Please provide me with any link that may help as soon as possible. Thanx alot, Rashrashetta
3
by: Zlatko Matić | last post by:
Hello. I'm wondernig what is happennig whith saved pass-through queries nested in regular JET query if regular JET query just filtrates result by start/end date...Does pass-through query first...
4
by: so.intech | last post by:
for example, ret = 0; for(i=0; i<3; i ++;) { for(j=0; j<4; j++;) { for(k=0; k<3; k++;) { for(m=0; m<4; m++;) {
6
by: duke442games | last post by:
Hi Gang I am working on fixing a corrupted database (A yearly proc did not handel the conversion from year to year and now I have to fix the resulting lines in the DB). Most of my fixes I have...
3
by: jonceramic | last post by:
Hi all, I'm not sure exactly what words to use or how to Google this. (I keep coming up with BOL links.) So, I'm going to ask the group for a starting point or proper terms to even describe...
3
by: lemonade134 | last post by:
Hello, I'm creating a database in Access 2003 after not using Access for about six years. I used to use nested queries, but don't see a way to do that in the newer form of Access. Instead, the books...
4
by: toddlahman | last post by:
I am using two while loops that are nested. The first loop (post name) returns the full column of results, but the second (post modified) only returns the first row of the column. Is there another...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.