在mysql的查询中创建链接

时间:2013-01-08 21:14:50

标签: php mysql hyperlink

我搜索了很多网站,但是我找不到如何使用mysql创建的查询创建链接。

我希望当有人点击coureurname时,他/她会被重定向到另一个页面,但我如何从这些查询结果中创建可点击链接。我在下面用php代码发布了我的查询,希望你们能帮助我,因为我真的不知道怎么做。

<html>
<title> lijstcoureur</title>
<head>
<H1> LIJST VAN COUREURS </H1>
</head>

<?php
$con=mysql_connect('localhost','root','****')
or die ('Kan geen verbinding maken met mySQL server');

$db=mysql_select_db('formule1',$con)
or die ('Kan de database niet selecteren');

$selectie=mysql_query("CALL lijstcoureurs()",$con);
if(!selectie)
die('Invalid query:'.mysql_error());
?>
<table border =1>

                        <thead>

                                <tr>
<?php

                                        for($fieldindex=0;$fieldindex<mysql_num_fields($selectie);$fieldindex++)

                                        {

                                                echo '<th>';

                                                echo mysql_field_name($selectie,$fieldindex);

                                                echo '</th>';

                                        }

                                        ?>

                                </tr>

                        </thead>

                        <tbody>

                                <?php

                                /*

                                Loop alle rijen langs en sla het resultaat op in variable $row */

                                        while($row=mysql_fetch_array($selectie))
                                        {


                                                // Begin een nieuwe rij
                                                echo('<tr>');

                                                // Loop alle rijen langs en zet de inhoud in de tabel
                                                for($fieldindex=0;$fieldindex<mysql_num_fields($selectie);$fieldindex++)

                                                {

                                                        echo('<td>');

                                                        echo($row[$fieldindex]);

                                                        echo('</td>');


                                                }

                                                echo('</tr>');

                                        }

                                ?>

                        </tbody>

                </table>

        </body>

</html>

3 个答案:

答案 0 :(得分:0)

假设echo($row[$fieldindex]);包含链接,您可以这样做:

echo "<a href=" . $row[$fieldindex] . ">Anchor text here</a>";

答案 1 :(得分:0)

将数据库中的查询结果放入href标记的<a>属性中。

例如:

<a href="<?php echo $queryResult; ?>"> My Query Result </a>

答案 2 :(得分:0)

我不确定你到底要做什么。您的表是否包含要显示为链接的URL?然后将它们包装在<a> - 标签中。另一方面,如果要在单击某一行时显示特殊页面(如详细视图),则必须实现该功能。

示例:

// Assuming $row contains the record's database ID at index 0
echo '<a href="/some_url/on/your/server?id=' . $row[0] . "'>Anchor text here</a>';

这将链接到特殊URL并在查询字符串中传递id。然后,您可以编写另一个PHP脚本来处理此URL,然后可以使用提供的ID($_GET['id'])检索有问题的记录并显示详细信息页面。

您可能希望研究PHP Web框架,因为您自己完成所有这些操作可能会很乏味,容易出错且容易受到攻击。良好的框架可以防止SQL注入,XSRF等事务,并抽象掉大部分数据库交互。