z4z07 Posted March 6, 2017 Share Posted March 6, 2017 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. Quote Link to comment https://forums.phpfreaks.com/topic/303366-select-name-from-a-table/ Share on other sites More sharing options...
cyberRobot Posted March 6, 2017 Share Posted March 6, 2017 What does your code look like? Quote Link to comment https://forums.phpfreaks.com/topic/303366-select-name-from-a-table/#findComment-1543827 Share on other sites More sharing options...
z4z07 Posted March 6, 2017 Author Share Posted March 6, 2017 I've tried somehting like this: <?php $result = mysqli_query($conn, "SELECT cat_name FROM domeniu_activitate INNER JOIN user_pers_juridica WHERE cat_id LIKE cat_id AND product_id = '$id'"); while($row = mysqli_fetch_array($result)) { echo $row['cat_name']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/303366-select-name-from-a-table/#findComment-1543828 Share on other sites More sharing options...
cyberRobot Posted March 6, 2017 Share Posted March 6, 2017 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 Quote Link to comment https://forums.phpfreaks.com/topic/303366-select-name-from-a-table/#findComment-1543830 Share on other sites More sharing options...
Solution Barand Posted March 6, 2017 Solution Share Posted March 6, 2017 (edited) 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 March 6, 2017 by Barand 1 Quote Link to comment https://forums.phpfreaks.com/topic/303366-select-name-from-a-table/#findComment-1543831 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.