473,395 Members | 1,348 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,395 software developers and data experts.

PHP MySQL two table question...

5
Here is my dilemma. I have two tables that i am wanting to read data from.

Table 1:
product_id
product_name
classification_id

Table 2:
classification_id
classification_name

What i want to be able to do is list all of the products in Table 1 but have the classification_name from Table 2 be used instead of the classification_id from Table 1.

As of right now i have it so it lists the classification_id for each product, but when i try to list the products with the classification_name it just shows the first classification_name for all of the products regardless of their classification_id

[PHP]<?php do { ?>
<tr align="left">
<td><!--//<?php echo $row_products['product_id']; ?>//-->
<?php echo $row_products['product_name'] ; ?>
</td>
<td align="center" valign="middle">
<?php echo $row_products['classification_id'] ; ?>

</td>
<?php } while ($row_products = mysql_fetch_assoc($products)); ?>[/PHP]

that is what i have so far. can anyone help me?
Feb 25 '08 #1
7 1812
harshmaul
490 Expert 256MB
you need an inner join my friend....

run this query...

Table 1: (tblProducts)
product_id
product_name
classification_id

Table 2: (tblClassification)
classification_id
classification_name

Expand|Select|Wrap|Line Numbers
  1. select tblProducts.product_id, tblProducts.product_name, tblClassification.classification_name
  2. From tblProducts
  3. inner join tblClassification on tblProducts.classification_id = tblClassification.classification_id
  4. Where 1=1
I hope that helps
Feb 25 '08 #2
dr9553
5
Ok but what do i put in my do while loop to have it display the correct information. Sorry i am new to some of this stuff.
Feb 25 '08 #3
You can try this way .
Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3.         $sql = "select tblProducts.product_id, tblProducts.product_name, tblClassification.classification_name From tblProducts inner join tblClassification on tblProducts.classification_id = tblClassification.classification_id Where 1=1";
  4.  
  5.         $result = mysql_query($sql);
  6.         while($row = mysql_fetch_assoc($result)){
  7.             echo $row['product_name']."<br>";
  8.             echo $row['classification_name'];
  9.         }
  10.       ?>
  11.  
Feb 25 '08 #4
dr9553
5
OMG! You are a genius! thank you so very very much for the help. I was ready to punch my monitor on friday cause nothing i tried was working!

THANK YOU!

Donald
Feb 25 '08 #5
dr9553
5
i even got it working for another category too! finally im learning something!
Feb 25 '08 #6
dr9553
5
Ok one last little thing with this. I have a drop down that i want to be able to select a brand from. this brand would then give me the list of products that i was asking about earlier. So when you select your brand this is the code i have:

[PHP]<?php if(isset($_GET['brand_id'])){ ?>[/PHP]
that then creates the table with all of the product data in it.

Right now the table that is created contains all of the products (which was what i was asking how to do before) but now i can not figure out how to get it to only list the products belonging to that brand.

Can anyone help. Also thank you again for all the help i have already recieved!
Feb 25 '08 #7
ronverdonk
4,258 Expert 4TB
If the brand name is in your product table:[php]
<?php
if(isset($_GET['brand_id'])) {
$brand = strip_tags($_GET['brand']);
// setup you MySQL
// select all products from requested brand
$sql="SELECT * FROM table_name WHERE brand_name='$brand'";
// execute statement
// retrieve and display all rows from result set
}
?>[/php]Ronald
Feb 25 '08 #8

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

Similar topics

11
by: Bruce A. Julseth | last post by:
Newbie Question: Is there a way to import MS Access into MySQL? Maybe create a CSV or something. If so, what would be the SQL to do this? Thanks... Bruce
19
by: Westcoast Sheri | last post by:
To keep track of how many fruits my visitors buy, I use a mySQL database (2 columns: "fruit" and "quantity")....so can we make these following mySQL queries work somehow? (visitor buys 5...
8
by: Tony Clarke | last post by:
Hi, Just a quick question about performance with MySQL & PHP. If I had a table in a MySQL database with about 100,000 records in it and I need to find the last record is there a quick way to do...
6
by: jacob nikom | last post by:
I would like to create data model for a group of stores. All stores in this group are very similar to each other, so it is natural to allocate one MySQL database per store. Each database is going...
0
by: Ed Smith | last post by:
I have two questions about REFERENCES: 1. It appears that mySQL treats REFERENCES associated with an attribute differently than FOREIGN KEY (<blah>) REFERENCES... Specifically, the first form...
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
7
by: Randy | last post by:
Folks: We have a web-based app that's _really_ slowing down because multiple clients are writing their own private data into a single, central database. I guess the previous programmer did...
6
by: ojorus | last post by:
Hi! My company make several flash-based games, and I use php to communicate with mysql to provide highscore-lists. My problem is this: When I save a player's score in the mysql-table, I want to...
6
by: fpcreator2000 | last post by:
Hello everyone. I'm having problems with a page I've created that is used to insert data into a database. It uploads two files into two distinct folder, and it takes the filenames and inserts...
23
by: MikeB | last post by:
I'm learning PHP and MySQL. In the samples I work with, the SQL table names are "escaped" in accent grave (`) marks, not in single or double quotes. Can anyone explain that? For instance, I...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.