Search This Blog

Thursday, July 2, 2009

MySQL Values From Two Tables - 2nd July

Recently I faced a problem with getting values from two(2) MySQL data at the same time. Using the common method of getting data will not work in Dreamweaver as the common method is only able to compare between:-

1 - URL Parameters w/ $_GET['x']
2 - Form variable w/ $_POST['x']
3 - Cookies w/ $_COOKIE['x']
4 - Session variable w/ $_SESSION['x']
5 - Server variable w/ $_SERVER['x']
6 - Entered value

Let me provide an example:-

You have table A to record user 'input items id' and you have another which displays the item full details such as name and price etc. And you have a webpage to display the items that the user has in the records. So, by calling the item id that the user has, the information of the particular item can be displayed without having to put all of the information into the 'input items id' form.

As you can see, none of the methods fits the purpose.
To do exactly what you wanted would require this MySQL query:-
(and they must be linked in the same recordset of it won't work)

SELECT item_info.*, user_saved.*
FROM (item_info LEFT JOIN user_saved ON user_saved.id_1=item_info.id)
WHERE user_id = colname

and the configurations for 'colname' would have to be:-
(After pressing "+" sign near 'variables' to add new variable)

Name:- colname
Type:- Integer
Default Value:- -1
Runtime Values:- $_GET['user_id']

And that's it! Run your page, click on the link with defined id and you've linked and obtained data from two totally different tables. Oh, and for all I know, this works with the entire recordset, meaning you can use the recordset 'bindings' as you wish. Email me if you're unsure of what is written above.

No comments: