0

given the following code:

   <?php
include "connect.php";
$query = "select d.Name,d.Image,d.Main_Style,g.ID,g.Name from dj d inner join genres g     on g.ID=d.Main_Style  order by d.Name";

$exec = $mysql->query($query) or die("Erreur");
$n = $exec->num_rows;

if($n > 0)
{
    echo '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
      <html><table>';

for($i=0;$i<$n;$i++)
{
    $row = $exec->fetch_array();

      //echo '<tr><td></td><td><a href='DJ.php?id=$row[ID]'>$row[Name]</a></td><td>$row[Main_Style]</td></tr>';
      echo "<tr><td><img src=\"$row[d.Image]\" width=\"100\" height=\"100\"/></td><td><a href='DJ.php?id=$row[ID]'>$row[Name]</a></td><td>$row[Main_Style]</td></tr>";
}
echo '</table></html>';
}


include "disconnect.php";
?>

as you can see i used inside the php code $row[d.image] to display the content of image in the table d , however there is an error , how to solve this ? >$row[Name] and the other variables are working just fine

1 Answer 1

1

The field "d.Image" in the MySQL query doesn't transfer to the result, the table name is removed.

The respective column in the result returned by MySQL is named only "Image".

To solve your problem, change $row[d.Image] to simply $row[Image] like you're doing with the other fields (ID, Name and Main_Style).

Sign up to request clarification or add additional context in comments.

4 Comments

ah ok , this seems to work fine , but what if i have 2 columns with the same name from two different tables ?
If you have two columns in the result with the same name you should use an alias in the query (for example d.Image as Image1, g.Image as Image2). Then you can use $row[Image1] and $row[Image2] in your php code.
Perfect ! appreciate it , didn't know that Aliases can work in this case .
You're welcome. One of the uses for aliases are exactly this one. To differentiate multiple columns that have the same name, when joining tables.

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.