Jump to content

Recommended Posts

Hello.

 

If I am retrieving data from multiple tables using a join and two of the tables have fields that are the same name how do I spit it back out in PHP?

 

e.g.

 

mysql_select_db(\"woohoo\");

$query = \'select * from table1, table2 where table1.website = table2.website;

$result = mysql_query($query);

$num_results = mysql_num_rows($result);

 

for ($i=0; $i<$num_results; $i++)

{

  • $row = mysql_fetch_array($result);

echo stripslashes($row[\"url\"]);

echo stripslashes($row[\"name\"]);

  • }

 

 

Both tables have a url value. I\'ve tried using \"table1.url\" instead of \"url\" but to no avail. If I just use \"url\" it gives me the later \"table2.url\"...possibly writing over?

 

Any suggestions, smart a** comments or slaps in the head are welcome.

 

Thanks,

 

rkimball

Link to comment
https://forums.phpfreaks.com/topic/1022-syntax-on-retrieval-of-data/
Share on other sites

Be specific in your select clause, so you just have the one \'url\' field from the table you want

 

[php:1:f4a47b0d76]<?php

mysql_select_db(\"woohoo\");

$query = \'select a.url, b.name,b.website from table1 a, table2 b where a.website = b.website;

$result = mysql_query($query);

$num_results = mysql_num_rows($result);

 

for ($i=0; $i<$num_results; $i++)

{

$row = mysql_fetch_array($result);

echo stripslashes($row[\"url\"]);

echo stripslashes($row[\"name\"]);

}

?>[/php:1:f4a47b0d76]

if you want both urls, add ..., b.url as urlB ...

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.