Jump to content

SOLVED echo'ing two colums from one row...


JP128

Recommended Posts

Okay, say I have a table called links. And inside links are two different columns called linkroute, and linkname. I want all of the rows to be put out into the format:

<a href="$LINKROUTE1">$LINKNAME1</a>
<a href="$LINKROUTE2">$LINKNAME2</a>

and so on so forth...

If someone could tell me how to do this, that would be great. Thanks for your time...
Link to comment
https://forums.phpfreaks.com/topic/30185-solved-echoing-two-colums-from-one-row/
Share on other sites

[code]
<?php
  // connect to db.
  $sql = "SELECT linkroute,linkname FROM links";
  if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result) > 0) {
      while ($row = mysql_fetch_assoc($result)) {
        echo "<a href=\"{$row['linkroute']}\">{$row['linkname']}</a>";
      }
    }
  }
?>
[/code]
you could add some error checking into the code that thorpe gave you

[code=php:0]// connect to db.
  $sql = "SELECT linkroute,linkname FROM links";
  if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result) > 0) {
      while ($row = mysql_fetch_assoc($result)) {
        echo "<a href=\"{$row['linkroute']}\">{$row['linkname']}</a>";
      }
    }
else{
echo "no links found";
}
  }
  else{
  echo "problem with query";
  }[/code]


I would do it slightly differently , but thats just a difference of coding styles, nothing major


[code=php:0] // put your db connection info here
  $r = mysql_query("SELECT * FROM links") or die(mysql_error());
  if(mysql_num_rows($r) > 0){
while($row = mysql_fetch_array($r){
$linkroute = $row['linkroute'];
$linkname = $row['linkname']; ?>
<a href="<?=$linkroute?>"><?=$linkname?></a>
<? }
}
else{
echo "No Links Found";
}[/code]


Assuming short tags enabled, if not replace <? with <?php

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.