Forums
This topic is locked
MySQL JOIN Query to show in recordset
Posted 03 Jul 2007 08:37:34
1
has voted
03 Jul 2007 08:37:34 Dallas Moore posted:
I have created two tables in this Truck Accessory database.Table1 contains the products.
Table2 defines the product categories.
Table1: PRODUCTS
------------------------------------------------------------
| id | product_name | category_id | description | price |
------------------------------------------------------------
|20567 | Bull Bar | 1 | Liberty BB | 650.00| (sample data)
------------------------------------------------------------
Table2: CATEGORY
--------------------
| id | category_name |
--------------------
| 1 | Grille Guards |
----------------------
I used a number to define the categories to eliminate long repeating data. I then included that defined number (category_id) in the category field of the products table.
Here is what I need to do.
I need to join table1(products) with table2(category) WHERE products.category_id = category.id
so on the webpage it will display the category name instead of the id like below:
-----------------------------------------------------------------------------------------------------
products.id | products.product_name | category.category_name | products.description | products.price|
-----------------------------------------------------------------------------------------------------
What would be the MySQL query and what would be the php code to display the data correcly?
Replies
Replied 03 Jul 2007 17:39:28
03 Jul 2007 17:39:28 Alan C replied:
Looks like you have almost written the join <img src=../images/dmxzone/forum/icon_smile.gif border=0 align=middle> without knowing it
What you want is something like this, first put the query into a string, then use that to access the database
<pre id=code><font face=courier size=2 id=code> $query_properties = sprintf("SELECT mgrs_firstName, mgrs_lastName, p_name, p_town, p_postcode, p_email, p_entry_type FROM properties INNER JOIN mgrs ON properties.p_userid = mgrs.mgrs_userid WHERE p_id = %s", $p_id); // $p_id contains a value
$properties = mysql_query($query_properties, $abc_connect) or die(mysql_error());</font id=code></pre id=code>
check out the full syntax for the INNER JOIN and then change the names in this example to suit your own. INNER JOIN joins the tables on the values you specify, there are also RIGHT and LEFT joins - there's an example here
www.htmlite.com/mysql012.php
What you want is something like this, first put the query into a string, then use that to access the database
<pre id=code><font face=courier size=2 id=code> $query_properties = sprintf("SELECT mgrs_firstName, mgrs_lastName, p_name, p_town, p_postcode, p_email, p_entry_type FROM properties INNER JOIN mgrs ON properties.p_userid = mgrs.mgrs_userid WHERE p_id = %s", $p_id); // $p_id contains a value
$properties = mysql_query($query_properties, $abc_connect) or die(mysql_error());</font id=code></pre id=code>
check out the full syntax for the INNER JOIN and then change the names in this example to suit your own. INNER JOIN joins the tables on the values you specify, there are also RIGHT and LEFT joins - there's an example here
www.htmlite.com/mysql012.php