# \[PHP/MySQL\]making links

**URL:** <https://forum.kirupa.com/t/php-mysql-making-links/189870>\
**Category:** programming\
**Created:** [June 6, 2006, 8:44pm UTC](https://forum.kirupa.com/t/php-mysql-making-links/189870 "2006-06-06T20:44:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![blazes](https://avatars.discourse-cdn.com/v4/letter/b/35a633/32.png) [@blazes](https://forum.kirupa.com/u/blazes)\
**Post date:** [June 6, 2006, 8:44pm UTC](https://forum.kirupa.com/t/php-mysql-making-links/189870/1 "2006-06-06T20:44:04Z")

</div>

Hello. I have connected to my database, and I have it print out the data it gets. but How do I make it so that instead just printing the text it makes it a link to a URL that includes part of another colom in the same row? Like the forums use. \

---

<div class="post-metadata">

**Author:** ![bwh2](https://avatars.discourse-cdn.com/v4/letter/b/8c91f0/32.png) [@bwh2](https://forum.kirupa.com/u/bwh2)\
**Post date:** [June 6, 2006, 8:48pm UTC](https://forum.kirupa.com/t/php-mysql-making-links/189870/2 "2006-06-06T20:48:17Z")

</div>

assuming $link\_url and $link\_title are your variables from the table…

```php
echo '<a href="'.$link_url.'" title="'.$link_title.'">'.$link_title.'</a>';

```

---

<div class="post-metadata">

**Author:** ![blazes](https://avatars.discourse-cdn.com/v4/letter/b/35a633/32.png) [@blazes](https://forum.kirupa.com/u/blazes)\
**Post date:** [June 6, 2006, 9:30pm UTC](https://forum.kirupa.com/t/php-mysql-making-links/189870/3 "2006-06-06T21:30:51Z")

</div>

well it kinda works now, but when it test it, it uses the word ‘.$link\_title.’ instead of the accuall title from the database.☹

---

<div class="post-metadata">

**Author:** ![bwh2](https://avatars.discourse-cdn.com/v4/letter/b/8c91f0/32.png) [@bwh2](https://forum.kirupa.com/u/bwh2)\
**Post date:** [June 6, 2006, 9:49pm UTC](https://forum.kirupa.com/t/php-mysql-making-links/189870/4 "2006-06-06T21:49:16Z")

</div>

right. the example i wrote assumes that’s what the variable is. you said you could echo out the data correctly, so i assumed you would know to replace the variable names i gave you with the correct ones from the db.

what is your php that you use to echo out the data from the db?

---

<div class="post-metadata">

**Author:** ![blazes](https://avatars.discourse-cdn.com/v4/letter/b/35a633/32.png) [@blazes](https://forum.kirupa.com/u/blazes)\
**Post date:** [June 6, 2006, 9:56pm UTC](https://forum.kirupa.com/t/php-mysql-making-links/189870/5 "2006-06-06T21:56:49Z")

</div>

\<?php  
// Connect to server and select databse  
mysql\_connect("$host", “$username”, “$password”)or die(“cannot connect”);  
mysql\_select\_db("$db\_name")or die(“cannot select DB”);

$sql=“SELECT \* FROM $tbl\_name WHERE forum\_id = 39 ORDER BY topic\_id desc limit 5”;  
$result=mysql\_query($sql);

// Define $color=1  
$color=“1”;

echo ‘\<table width=“400” border=“1” align=“center” cellpadding=“2” cellspacing=“0”\>’;  
while ($rows=mysql\_fetch\_array($result)){

// If $color==1 table row color = #FFC600  
if($color==1){  
echo “\<tr bgcolor=’#FFC600’\>  
\<td\>”.$rows[‘topic\_title’]."\</td\>  
\</tr\>";  
// Set $color==2, for switching to other color  
$color=“2”;  
}

// When $color not equal 1, use this table row color  
else {  
echo “\<tr bgcolor=’#ff9900’\>  
\<td\>”.$rows[‘topic\_title’]."\</td\>  
\</tr\>";  
// Set $color back to 1  
$color=“1”;  
}

}  
echo ‘\</table\>’;  
mysql\_close();  
?\>

I did put in my own variables. but it did as I said. this makes it go like this: [www.blazes816.com/news.php](http://www.blazes816.com/news.php). But where the titles are (topic\_title) I want those to be links, linking to [www.blazes816.com/forums/veiwtopic.php?t=](http://www.blazes816.com/forums/veiwtopic.php?t=)(here I need the number from the column ‘topic\_id’)

Please help if you can. I’ve been working on this for 3 days.

---

<div class="post-metadata">

**Author:** ![bwh2](https://avatars.discourse-cdn.com/v4/letter/b/8c91f0/32.png) [@bwh2](https://forum.kirupa.com/u/bwh2)\
**Post date:** [June 6, 2006, 10:11pm UTC](https://forum.kirupa.com/t/php-mysql-making-links/189870/6 "2006-06-06T22:11:32Z")

</div>

ah yes, a classic example of using the wrong quotation marks. you should be using single quotes for strings. that way, you can use double quotes within your strings without having to use escape characters. also, in html, the attributes for tags should be set within double quotes. here is the middle part of your code fixed up.

```php
<?php
//the beginning...

// If $color==1 table row color = #FFC600
if($color==1){
echo '<tr bgcolor="#FFC600">';
echo '<td>'.$rows['topic_title'].'</td>';
echo '</tr>';

// Set $color==2, for switching to other color
$color="2";
}

// When $color not equal 1, use this table row color
else {
echo '<tr bgcolor="#ff9900">';
echo '<td><a href="http://www.blazes816.com/forums/viewtopic.php?t='.$rows[topic_id].'" title="'.$rows[topic_title].'">'.$rows[topic_title].'</td>';
echo '</tr>';
// Set $color back to 1
$color="1";
}

//... the rest
?>

```

---

<div class="post-metadata">

**Author:** ![blazes](https://avatars.discourse-cdn.com/v4/letter/b/35a633/32.png) [@blazes](https://forum.kirupa.com/u/blazes)\
**Post date:** [June 6, 2006, 10:33pm UTC](https://forum.kirupa.com/t/php-mysql-making-links/189870/7 "2006-06-06T22:33:35Z")

</div>

THANK YOU SO MUCH! IT WORKS! IT WORKS!

don’t take this the wrong way, but I love you.

---

<div class="post-metadata">

**Author:** ![bwh2](https://avatars.discourse-cdn.com/v4/letter/b/8c91f0/32.png) [@bwh2](https://forum.kirupa.com/u/bwh2)\
**Post date:** [June 6, 2006, 11:30pm UTC](https://forum.kirupa.com/t/php-mysql-making-links/189870/8 "2006-06-06T23:30:09Z")

</div>

no problem.
