Jump to content

Recommended Posts

Hi,

 

I'm very new to PHP and have managed to get through most of the problems I've encountered so far, but I have got to one point that I can't seem to solve.

 

I have two mysql tables (Categories & Products), I have a page that shows the categories (generated from the categories table) and when a category is selected it lists the products in that category.

 

Due to stock etc. not all categories always have products associated to them.

 

The Category & Product Tables are automatically updated, so what I need to do is only show categories that have products associated to them, both tables have a column category_no, and nothing I've tried so far seems to work.

 

The code I have so far is:-

 

Require_once('config.php');

Mysql_connect(db_host, db_user, db_password);

@mysql_select_db(db_database) or die( "unable to select database");

$query="select * from categories";

$result=mysql_query($query);

$num=mysql_numrows($result);

Mysql_close();

Thanks

 

Pete

Link to comment
https://forums.phpfreaks.com/topic/227650-querying-multiple-tables/
Share on other sites

If all you need is a count, a SELECT COUNT() query against an indexed field  is much more efficient than selecting every field of every record like you're doing. If you must do it that way however, the function is mysql_num_rows(), not mysql_numrows().

Are you thinking something like

 

SELECT categories.*, products.* FROM categories JOIN products ON categories.id = products.id WHERE products.numberinstockfield > 0

 

I'm sure there are ways to improve that, I just wanted to give you the idea.

MAQ - sorry, first post, didn't realize.  The Query does work, $num is then used for the output and what's displayed, filtering what's displayed is what I need to do..

 

Pikachu2000 - As I said, I'm very new to this and that may work, but it means nothing to me???

 

MatthewJ - I'll have a try at that and see how it goes, thanks.

 

Pete

If category_no in tbl_categories doesn't match with any category_no's in tbl_products then remove from results.

Then only match on that condition.  Something like:

SELECT * FROM tbl_categories tc LEFT JOIN tbl_products tp ON [whatever_field(s)_you_join_on] WHERE tc.category_no = tp.category_no

If category_no in tbl_categories doesn't match with any category_no's in tbl_products then remove from results.

Then only match on that condition.  Something like:

SELECT * FROM tbl_categories tc LEFT JOIN tbl_products tp ON [whatever_field(s)_you_join_on] WHERE tc.category_no = tp.category_no

 

Thanks, I'll have a play with that tomorrow and let you know how it goes.

 

Pete

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.