per källström per källström - 2 months ago 7
HTML Question

php echo link with variables from database

Im trying to link to a post with variables from my database, i guess im missing some ' or something... cant get it right it seems. Anyone here that got any idea?

<?php
require_once 'includes/conn.php';
try{
$conn = new PDO("mysql:dbname=$db;host=$server;port=$port","$user","$pass");
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sql = "SELECT * FROM $table WHERE NOT forum_id = 2 ORDER BY topic_id desc";
$stmt = $conn->prepare($sql);
$stmt->execute ();
foreach ($conn->query($sql) as $post) {
echo '<a href="http://forum.mysite.com/viewtopic.php?f='.$post[forum_id].'&t='.$post[topic_id].'">', $post[subject], '</a>';
}
}catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
$conn=null;
?>


This is the problem:

echo '<a href="http://forum.mysite.com/viewtopic.php? f='.$post[forum_id].'&t='.$post[topic_id].'">', $post[subject], '</a>';


the link should look something like this:

http://forum.mysite.com/viewtopic.php?f=12&t=12

Answer

You should use . for concatenation (not comma)

echo '<a href="http://forum.mysite.com/viewtopic.php?f='. 
  $post[forum_id].'&t='.$post[topic_id].'">' .  
      $post[subject]  .'</a>';

and remove the blank between ? and f