-2

i'm using this code to display the latest 5 entries from a database on my website. Now i want to show the $result as links to ex. pedigree.php?id=$resultname$&db=pedigree&gens=5. How to do that?

<?php
    $result = mysql_query("SELECT Name FROM pedigree ORDER BY PedigreeId DESC LIMIT 5");

    while($row = mysql_fetch_row($result))
        echo $row[0].'<br />';

?>

Thanks a lot for your help!

Legendi
  • 21
  • 4
  • You already have the code to display it in a table. Is it really that far a stretch to figure out how to apply it to a URL? – Flosculus Dec 09 '15 at 15:39
  • 1
    What is the content of `$row[0]`? Also, you shouldn't use `mysql_*` functions - they are deprecated and no longer supported in the latest version of `PHP`. Look at `mysqli_*` or using `PDO` prepared statments. – Ben Dec 09 '15 at 15:39
  • I'm really a newbie and copy/pasted this code from somewhere else on the internet. The content of $row[0] is a name. – Legendi Dec 09 '15 at 15:42
  • column names are usually case-sensitive in MySQL if your table is setup to be that. So, in a fetch row such as `['Name']` or `['name']` as suggested below, stand to fail. check for errors on your query http://php.net/manual/en/function.mysql-error.php - I also don't know why they suggested row names like `Name` or `name` being mixed, and whether it really exists in your table or not. – Funk Forty Niner Dec 09 '15 at 15:52
  • Plus, do read http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1 your code is open to a serious SQL injection using a GET method like that. – Funk Forty Niner Dec 09 '15 at 15:58

2 Answers2

0

This is a basic question , You should serach google for it first :) .. anyway here is the answer

<?php
    $result = mysql_query("SELECT * FROM pedigree ORDER BY PedigreeId DESC LIMIT 5");

    while($row = mysql_fetch_row($result)){

            echo '<a href="pedigree.php?id='.$row['name'].'$&db='.$row['PedigreeId'].'&gens='.$row['name'].'">'.$row['name'].'</a><br />';
    }


?>

One Advice: Do not use mysql_* functions ,use mysqli or PDO .. mysql_* functions are depreciated in updated versions of PHP

Aniruddha Chakraborty
  • 1,849
  • 1
  • 20
  • 32
0

A link in HTML has the syntax <a href="url.php">Link Text</a>.

So:

while($row = mysql_fetch_row($result)){
    echo "<a href='pedigree.php?id={$row[0]['Name']}&db=pedigree&gens=5'>{$row[0]['Name']}</a>";
}

FYI, you shouldn't be using mysql_* functions as they are deprecated and no longer supported in PHP 7.*

Ben
  • 8,894
  • 7
  • 44
  • 80