Jump to content

Select name from a table!


Go to solution Solved by Barand,

Recommended Posts

Hi guys!

 

I have 2 mysql tables 1 named categories and the other one named products. In the first table i have cat_id, cat_name and in the secoudn one i have product_id, product_name, product_price, cat_id

 

Now on the product page where i have my product listed i wanna display the cat_name based on the cat_id wich is present in both tables. What is the correct mysql syntax for this.

 

Thank you very much.

Link to comment
https://forums.phpfreaks.com/topic/303366-select-name-from-a-table/
Share on other sites

You would use the table names to specify the column you want to use. For example

    domeniu_activitate.cat_id

 

and

    user_pers_juridica.cat_id

 

 

For shorter table names, you create table aliases. More information can be found here:

https://dev.mysql.com/doc/refman/5.7/en/select.html

  • Solution

Your join syntax is wrong.

SELECT cat_name 
FROM domeniu_activitate d
INNER JOIN user_pers_juridica u
    ON d.cat_id = u.cat_id  
WHERE product_id = '$id'

It is also better to use prepared queries. You should not be putting data ($id) directly into your queries.

Edited by Barand
  • Like 1
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.