Tom
Tom

Reputation: 19

How to retrieve id for each row and then assign it to a link

$connect = mysql_connect("localhost", "username", "password");
if($connect) {
            mysql_selectdb('phplogin');
            $query = mysql_query("SELECT * FROM forum ORDER BY time desc");
            $query2 = mysql_query("SELECT * FROM forum");
            $data2 = mysql_fetch_array($query2);
            $id = $data2['id'];


            while($data = mysql_fetch_array($query)) {
                echo "<div class='post'>" . "<div class='leftside'>" . "<h3 class='by'>" . $data['user'] . "</h3>" . "<h5 class='date'>" . $data['time'] . "</h5>" . "</div>" . "<div class='after'>" . "</div>" . "<div class='rightside'>" . "<h2 class='title'>" . "<a href='Forum.php?$id'>" . htmlspecialchars($data['title']) . "</a>" . "</h2>" . "<p class='description'>" . htmlspecialchars($data['description']) . "</p>"  . "</div>" . "<div class='clear'>" . "</div>" . "</div>";
            }

This is what ive tried but it assigns the first rows id to all links and i don't know how to do it, i am new to php and mysql thanks for the help in advance.

Upvotes: 1

Views: 53

Answers (1)

kero
kero

Reputation: 10638

The problem is that you are saving the id in $id - a variable. This can hold only one value.

Why are you even doing this? Both your queries are selecting from forum, can't you use $data['id']?

while($data = mysql_fetch_array($query)) {
    echo "<div class='post'>" . "<div class='leftside'>" . "<h3 class='by'>" . $data['user'] . "</h3>" . "<h5 class='date'>" . $data['time'] . "</h5>" . "</div>" . "<div class='after'>" . "</div>" . "<div class='rightside'>" . "<h2 class='title'>" . "<a href='Forum.php?".$data['id']."'>" . htmlspecialchars($data['title']) . "</a>" . "</h2>" . "<p class='description'>" . htmlspecialchars($data['description']) . "</p>"  . "</div>" . "<div class='clear'>" . "</div>" . "</div>";
}

Keep in mind that mysql_* functions are officially deprecated and hence should not be used in new code. You can use PDO or MySQLi instead. See this answer on SO for more information.


Doing this with MySQLi and printf() could look like this.

$link = mysqli_connect("localhost", "username", "password", "dbname");
/* check connection */
if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
}

$query = mysql_query("SELECT * FROM forum ORDER BY time desc");

if ($result = mysqli_query($link, $query)) {
        /* fetch associative array */
        while ($data = mysqli_fetch_assoc($result)) {
                printf ("<div class='post'><div class='leftside'>
                    <h3 class='by'>%s</h3>
                    <h5 class='date'>%s</h5></div><div class='after'></div><div class='rightside'>
                    <h2 class='title'><a href='Forum.php?%u'>%s</a></h2>
                    <p class='description'>%s</p></div><div class='clear'></div></div>",
                    $data['user'],
                    $data['time'],
                    $data['id'],
                    htmlspecialchars($data['title']),
                    htmlspecialchars($data['description']));
        }

        /* free result set */
        mysqli_free_result($result);
}

Upvotes: 1

Related Questions