0

I got a query that joined some tables together, some have identical names, for example id is twice in the result, and when I try to echo the id, it displays the wrong one.

How can I solve that?

My query:

$content = "
SELECT ct.* 
     , fe.* 
     , cn.*
  FROM web_content ct
  JOIN web_fieldsandfilters_elements fe 
    ON fe.item_id = ct.id
  JOIN web_fieldsandfilters_connections cn 
    ON cn.element_id = fe.id
 WHERE ct.alias = '".$conn->real_escape_string($_GET['alias'])."' 
   AND ct.state = 1 
 ORDER 
    BY ct.ordering
";

I would like to be able to just display it like this:

echo $contentcr[0]['id_ct'];

I want to still retrieve everything from web_content (which I named ct) but also name the id row differently. How can I do that?

Strawberry
  • 33,750
  • 13
  • 40
  • 57
twan
  • 2,450
  • 10
  • 32
  • 92
  • $content = "SELECT ct . * AS jellybeans FROM web_content..." – Jacey Nov 17 '16 at 13:02
  • 1
    Use column aliases. – Paul Spiegel Nov 17 '16 at 13:05
  • @Jacey I get a syntax error when using that. – twan Nov 17 '16 at 13:30
  • @twan, that was a guideline, not a full answer. RIght now, you're selecting your columns based on what they're named in the database, which is why you're getting a conflict since more than one table contains a column of that name. As Paul Spiegel suggested, use column aliases. Here's a neat tuorial: http://www.mysqltutorial.org/mysql-alias/ – Jacey Nov 17 '16 at 13:45

0 Answers0