per källström per källström - 10 months ago 44
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?

require_once 'includes/conn.php';
$conn = new PDO("mysql:dbname=$db;host=$server;port=$port","$user","$pass");
$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="'.$post[forum_id].'&t='.$post[topic_id].'">', $post[subject], '</a>';
}catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";

This is the problem:

echo '<a href=" f='.$post[forum_id].'&t='.$post[topic_id].'">', $post[subject], '</a>';

the link should look something like this:

Answer Source

You should use . for concatenation (not comma)

echo '<a href="'. 
  $post[forum_id].'&t='.$post[topic_id].'">' .  
      $post[subject]  .'</a>';

and remove the blank between ? and f