Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am running a SELECT query and it has a.id and c.id like so:

$sql = "
(SELECT
a.id,
a.Title,
a.Category,
c.id,
c.cateName FROM ads_list
AS a LEFT JOIN ads_cate
AS c ON c.id=a.Category WHERE a.Category = $CatID)"; // $CatID is defined earlier in     the script but is not pertinent to this problem.

$result = mysql_query($sql) or die(mysql_error());

while ($row = mysql_fetch_assoc($result)) { echo $row['id'] // equals c.id }

But $row['id'] is the last id called in the query and evaluates to c.id. I need to get a.id in my while loop.

My question is how do I get the value of a.id and c.id in my while() loop uniquely?

share|improve this question
1  
This is a good illustration of one reason why it's not good to name your primary key id in every table. –  Bill Karwin Aug 4 '11 at 22:09
 
Bill, thanks for that tip. –  OldWest Aug 4 '11 at 22:12
1  
mine are all called id, how hard is it to use an alias (not very) –  Dagon Aug 4 '11 at 22:14
add comment

6 Answers

up vote 3 down vote accepted

Give an alias to the c.id column.

Try this:

$sql = "
(SELECT
a.id,
a.Title,
a.Category,
c.id AS c_id,
c.cateName FROM ads_list
AS a LEFT JOIN ads_cate
AS c ON c.id=a.Category WHERE a.Category = $CatID)"; 


$result = mysql_query($sql) or die(mysql_error());

while ($row = mysql_fetch_assoc($result)) { 
 echo $row['id'];
 echo $row['c_id'];
}
share|improve this answer
add comment
$sql = "
(SELECT
a.id as AID,
a.Title,
a.Category,
c.id as CID,
c.cateName FROM ads_list
AS a LEFT JOIN ads_cate
AS c ON c.id=a.Category WHERE a.Category = $CatID)";
share|improve this answer
add comment
SELECT
  a.id AS aid,
  a.Title
  a.Category
  c.id AS cid,
  c.cateName
FROM ads_list a 
   LEFT JOIN ads_cate c ON c.id=a.Category WHERE a.Category = $CatID)

Have fun

share|improve this answer
add comment

You have two options:

1- give each id an alias:

SELECT
a.id AS aid,
a.Title,
a.Category,
c.id AS cid,
...

and address them as $row[ 'aid' ] and $row[ 'cid' ].

2- use mysql_fetch_row() instead of mysql_fetch_assoc() and address them as $row[0] and $row[3].

share|improve this answer
add comment

you can change your query to something like:

SELECT
a.id as a_id,
a.Title,
a.Category,
c.id as c_id,
c.cateName FROM ads_list
AS a LEFT JOIN ads_cate
AS c ON c.id=a.Category WHERE a.Category = $CatID

that way there is no ambiguity in column names and you can access it as $row['a_id'] or $row['c_id']

share|improve this answer
add comment

Can't you just remove c.id from query? You don't need that within SELECT statement if you need it in your query:

$sql = "SELECT a.id, a.Title, a.Category, c.cateName 
FROM ads_list AS a 
LEFT JOIN ads_cate AS c ON c.id=a.Category 
WHERE a.Category = $CatID"; 

$result = mysql_query($sql) or die(mysql_error());

while ($row = mysql_fetch_assoc($result)) { echo $row['id'] // equals c.id }

If you need it though, you can also use PDO and value binding. I do not recommend using mysql_* functions. PDO is just better and easier to read:

  $query = $pdo->query('SELECT a.id, a.Title, a.Category, c.id, c.cateName 
                               FROM ads_list AS a 
                               LEFT JOIN ads_cate AS c ON c.id=a.Category 
                               WHERE a.Category = $CatID');

  $query->bindColumn(1, $id_a); //numerical index goes from 1
  $query->bindColumn(4, $id_b);
  $query->bindColumn('Title', $title); //you can also index using column name if it's possible

  while ($query->fetch(PDO::FETCH_BOUND)) //fetch returns true if there are any records to fetch
  {
    echo $id_a;
    echo $id_b;
    echo $title;
  }

Downside of this solution (and mysql_fetch_row) is that any change within the query can result in change of indexes. And even if now it's not the problem, believe me - I've seen a query with 30 returned columns and mysql_fetch_row applied. It was a nightmare. Impossible to find out which column in database is represented by certain variable.

share|improve this answer
 
Thanks for the detailed explanation. –  OldWest Aug 5 '11 at 20:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.