0

I have got an inner join working how I want it to when I do an sql query in my database.

SELECT debate.date, school1.name, school2.name
FROM debate
INNER JOIN schools as school1
ON debate.homeid=school1.id
INNER JOIN schools as school2
ON debate.awayid=school2.id

Returns

date        name        name
19-01-2013  st Peters   St Simons

When I try doing this with php. I use the same select statement and add in the below.

foreach ($result as $row)
{
  $debates[] = array(
    'date' => $row['date'],
    'awayschool' => $row['name'],
    'homeschool' => $row['name'],
  );
}


<table>
<tr>
<td><?php echo $debate['date']; ?></td>
<td><?php echo $debate['name']; ?></td>
<td><?php echo $debate['name']; ?></td>
</tr>
</table>

Which returns

date        name        name
19-01-2013  st Peters   st Peters

However I want it to return.

date        name        name
19-01-2013  st Peters   St Simons

In my array I have tried several different things such as replacing

'awayschool' => $row['name'],

with

'awayschool' => $row['school1.name'],

but I get an error saying undefined index when I do this.

What am I doing wrong here?

4 Answers 4

2

The issue here is that both school1.name and school2.name are (in php) both named $row['name'], you can fetch one using an AS to rename it

SELECT debate.date, school1.name, school2.name AS name2
FROM debate
INNER JOIN schools as school1
ON debate.homeid=school1.id
INNER JOIN schools as school2
ON debate.awayid=school2.id

Then change your foreach loop to this

foreach ($result as $row)
{
  $debates[] = array(
    'date' => $row['date'],
    'awayschool' => $row['name'],
    'homeschool' => $row['name2'],
  );
}
Sign up to request clarification or add additional context in comments.

Comments

1

Modify your query like so

SELECT debate.date, school1.name "School1Name", school2.name "School2Name"
FROM debate
INNER JOIN schools as school1
ON debate.homeid=school1.id
INNER JOIN schools as school2
ON debate.awayid=school2.id;

Then:

foreach ($result as $row)
{
  $debates[] = array(
    'date' => $row['date'],
    'awayschool' => $row['School1Name'],
    'homeschool' => $row['School2Name'],
  );
}

Comments

0

Just use an alias for them.Like school1.name AS SCHLNAME1, school2.name AS SCHLNAME2 then these Indexes will be available in your array.Then it will be

'awayschool' => $row['SCHLNAME1'],
'awayschool' => $row['SCHLNAME2']

Comments

0

Try changing the following

<td><?php echo $debate['date']; ?></td>
<td><?php echo $debate['name']; ?></td>
<td><?php echo $debate['name']; ?></td>

to this

 <td><?php echo $debate['date']; ?></td>
 <td><?php echo $debate['awayschool']; ?></td>
 <td><?php echo $debate['homeschool']; ?></td>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.