Jump to content


Photo

Syntax? on retrieval of data


  • Please log in to reply
2 replies to this topic

#1 rkimball

rkimball
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 16 September 2003 - 06:40 PM

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.

[quote]
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++)
{[list]$row = mysql_fetch_array($result);
echo stripslashes($row[\"url\"]);
echo stripslashes($row[\"name\"]);[list]}



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
Lava of moltenlava.com

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,015 posts

Posted 16 September 2003 - 07:11 PM

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 ...
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 rkimball

rkimball
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 17 September 2003 - 08:16 PM

Thanks, the code worked.

rkimball
Lava of moltenlava.com




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users