Richard Bell
Richard Bell

Reputation: 405

Echoing HTML links

If I am calling a list of links from a mysql table named categories I think I use a select statement like:

  $query = "SELECT cat_name, cat_description FROM categories";
  $result = mysqli_query($dbc, $query)
    or die('Error querying database.');

  while ($row = mysqli_fetch_array($result)){
    echo '$row';
  }

These categories have been joined with a business_info table in a mapping_table (they have a many to many relationship). I think that at the moment this will echo a string for each category name so I don't understand how I make my echoed results active links which will display the business info when clicked.

I know that once clicked they will issue a SELECT statement to the mapping_table which i'm cool with. the bit i'm struggling with is making them clickable to issue that statement. Any ideas, i'm lost!

EDIT: I edited out the AND clause. I was wrong.

Upvotes: 0

Views: 108

Answers (1)

Marc B
Marc B

Reputation: 360912

Your SQL is invalid. It shoudd be

SELECT cat_name, cat_description FROM categories

Your die() clause should be

... or die (mysql_error())

so you can see exactly what the error was (a fixed string is useless for debugging purposes). And then your retrieval loop should be:

while($row = mysql_fetch_assoc($result)) {
    echo <<<EOL
<a href="script.php?category={$row['cat_name']}">{$row['cat_description']}</a>

EOL;
}

Upvotes: 7

Related Questions