0

i want to fetch a field from database eg. "name" and then use it as a link to display further info about that name from the database itself i used -

while($re=mysql_fetch_array($result))
  {

echo"<a href=\"test.php?id=1\">{$re["name"]}'</a>'";

    }

but i want that each 'name' entry when fetched from database be assigned different id like- adam id=1 chris id=2 so that in the next page i can GET that id and display contents accordingly. please help.

3 Answers 3

1
while ($row = mysql_fetch_assoc($result)) {
  echo "<a href=\"test.php?id={$row['id']}\">".htmlspecialchars($row['name'])."</a>\n";
}

Assuming you have an id column in the database, all you need to do is include that field in the results of the SELECT query and echo it along with your existing code.

You should have an auto-incrementing primary key in your database that you will use in the SELECT query on your next page, and all you need to do is use this as your id.

If you don't have a numeric primary key, add one. If for some reason you can't, use the name field as the id and select by that instead.

A couple of side notes:

  • Make sure you escape the name field with htmlspecialchars() to ensure you don;t break your output with user entries.
  • Use mysql_fetch_assoc() instead of mysql_fetch_array() - this is the right thing to do 99% of the time.
  • You had extra single quotes surrounding your closing </a> - I have removed them in the above example.
Sign up to request clarification or add additional context in comments.

Comments

0

Editing your current example to keep a count like this:

$id = 0;

while($re=mysql_fetch_array($result)) {
     $id++;
     echo"<a href=\"test.php?id={$id}\">{$re["name"]}'</a>'";
}

1 Comment

Awsomeeee just what i wanted!Thankss!
0

It's easier to just use the name itself as the id, and then on the next page use the name as your database query WHERE clause. Since an integer id isn't known to your database, you cannot easily match it back up to the name.

while($re=mysql_fetch_array($result))
{
  $name = $rs['name'];
  echo"<a href='test.php?name='" . urlencode($name) . "'>" . htmlentities($name) . "</a>";
}

On your other page...

$name = urldecode($_GET['name']);
$name = mysql_real_escape_string($name);
$result = mysql_query("SELECT * FROM table WHERE name='$name');

This assumes, of course, that your names are unique.

4 Comments

i understood your idea but i didn't understand the code part of it?
thanks i tried but its not displaying anything on the second page:(
On the second page, you need to fill in the rest of what you want to do. I've only gotten you as far as the query. You need to retrieve the results and display them however necessary.
i did this -<?php $name = urldecode($_GET['name']); $name = mysql_real_escape_string($name); $result = mysql_query("SELECT * FROM page WHERE name='$name'"); while($re=mysql_fetch_array($result)) {echo $re['company'];

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.