TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 06-15-2009, 04:06 PM   #1 (permalink)
The Wanderer
 
shoaibmunir's Avatar
 
Join Date: Jun 2009
Location: Dubai, UAE
Posts: 23
Thanks: 2
shoaibmunir is on a distinguished road
Default Search query multiple tables? (PHP, MySQL)

I'm working on a php project. i have a create a search product page. its contain multi table and working fine but exact problem is both table have some same column names i can't get the first table column value


Database Structure //I can't change must i've to create a script for getting result


tbl_products:-
rn //i want this value because i'm using as a product id
imagename


tbl_productsdetail

rn //i don't wana this value
pn //value coming from tbl_products rn column
name
description



Code:
$apshmquery="select p.*, pd.* from tbl_products p, tbl_productsdetail pd where p.code like '%" . $apshmreqcode .  "%' or pd.name like '%" . $apshmreqq .  "%' or pd.description like '%" . $apshmreqq .  "%' and p.rn=pd.pn ";


 $apshmsql = mysql_query($apshmquery);

$apshmrows = mysql_num_rows($apshmsql);

while ($apshmrecords = mysql_fetch_array($apshmsql)) {

echo ($apshmrecords["rn"]."</br>");//i want here tbl_products column value

echo ($apshmrecords["name"]."</br>");//its coming from productsdetail working fine

echo ($apshmrecords["description"]."</br>");//its coming from productsdetail working fine

}

have any good solution??

Thank's in advance :)
Send a message via MSN to shoaibmunir
shoaibmunir is offline  
Reply With Quote
Old 06-15-2009, 04:17 PM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

You can use the AS keyword for that.

sql Code:
SELECT
    myTable1.id AS myTable1_Id,
    myTable2.id AS myTable2_Id
FROM
    ...

Please let me know if that makes sense. If not, I will explain much better !
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 06-15-2009, 04:20 PM   #3 (permalink)
The Wanderer
 
shoaibmunir's Avatar
 
Join Date: Jun 2009
Location: Dubai, UAE
Posts: 23
Thanks: 2
shoaibmunir is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
You can use the AS keyword for that.

sql Code:
SELECT
    myTable1.id AS myTable1_Id,
    myTable2.id AS myTable2_Id
FROM
    ...

Please let me know if that makes sense. If not, I will explain much better !
thank's for your reply but you have to explain me because i have already try with same query
Send a message via MSN to shoaibmunir
shoaibmunir is offline  
Reply With Quote
Old 06-15-2009, 05:44 PM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

If you have conflicting columns then you will, unfortunately, need to specify them individually as in the code example.

To take your code as the example this time, you would have to specify explicitly that you would like the id from the tbl_products table, with your supplied alias, p. You would specify this by adding the following to your SELECT statement:

sql Code:
p.rn AS rn_id

Which, excluding your conditionals at the end of your MySQL statement above, would now be like so:

sql Code:
SELECT
    p.*,
    pd.*,
    p.rn AS rn_id
FROM
    tbl_products p,
    tbl_productsdetail pd

All your data is still returned, but the ID you require is now stored in rn_id, because rn_id is unique, whereas id is not unique to that particular table, and therefore overwritten.

So then to get the value you would do:

php Code:
echo $apshmrecords['rn_id'];
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 06-16-2009, 11:49 AM   #5 (permalink)
The Wanderer
 
shoaibmunir's Avatar
 
Join Date: Jun 2009
Location: Dubai, UAE
Posts: 23
Thanks: 2
shoaibmunir is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
If you have conflicting columns then you will, unfortunately, need to specify them individually as in the code example.

To take your code as the example this time, you would have to specify explicitly that you would like the id from the tbl_products table, with your supplied alias, p. You would specify this by adding the following to your SELECT statement:

sql Code:
p.rn AS rn_id

Which, excluding your conditionals at the end of your MySQL statement above, would now be like so:

sql Code:
SELECT
    p.*,
    pd.*,
    p.rn AS rn_id
FROM
    tbl_products p,
    tbl_productsdetail pd

All your data is still returned, but the ID you require is now stored in rn_id, because rn_id is unique, whereas id is not unique to that particular table, and therefore overwritten.

So then to get the value you would do:

php Code:
echo $apshmrecords['rn_id'];

Thank dude its working fine but i'm not getting search query result 100%

Code:
select p.*, pd.*, p.rn as p_rn from tbl_products p, tbl_productsdetail pd where p.rn=pd.pn and p.code like '%2001%' or pd.name like '%a%' or pd.description like '%a%'
Send a message via MSN to shoaibmunir
shoaibmunir is offline  
Reply With Quote
Old 06-16-2009, 11:55 AM   #6 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

What do you mean by not 100%? What appears to not be working?
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
shoaibmunir (06-23-2009)
Old 06-16-2009, 01:47 PM   #7 (permalink)
The Wanderer
 
shoaibmunir's Avatar
 
Join Date: Jun 2009
Location: Dubai, UAE
Posts: 23
Thanks: 2
shoaibmunir is on a distinguished road
Default

Thank's

Its Working Fine Now :)

Last edited by shoaibmunir : 06-23-2009 at 02:28 PM.
Send a message via MSN to shoaibmunir
shoaibmunir is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
MySQL query help sjaq Advanced PHP Programming 4 05-04-2009 10:33 AM
PHP & MySQL Inserting multiple entires from range. Acrylic Absolute Beginners 3 10-02-2008 02:27 AM
Building a MySQL search delayedinsanity MySQL & Databases 4 08-14-2008 04:21 AM
Securing your MySQL Queries with Sprintf Wildhoney General 26 03-18-2008 06:52 PM
Error in connecting to MySQL via PHP EyeDentify MySQL & Databases 0 01-03-2008 01:06 PM


All times are GMT. The time now is 11:41 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design