0

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

3
  • And what is the link it is giving...? Commented Sep 20, 2016 at 19:25
  • You have an extra space between ? and f=. Commented Sep 20, 2016 at 19:31
  • it is just blank :/ Commented Sep 20, 2016 at 19:33

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

are you sure you have proper value in the the vars ... update the your question and show the actual result using var_dump('<a href="forum.mysite.com/viewtopic.php? f='. $post[forum_id].'&t='.$post[topic_id].'">' . $post[subject] .'</a>')
comma can be used to separate multiple arguments to echo.
sigh... subject shou've been post_subject /facepalm
@perkällström what in var_dump ?.. update your question and show me .. the result

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.