2013-01-08 84 views
0

我已經搜索了很多網站,但是我找不到如何從我用mysql創建的查詢創建鏈接。在從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> 
+1

'mysql_ *'函數將[在PHP 5.5中棄用](http://php.net/manual/en/faq.databases.php#faq.databases.mysql.deprecated)。不建議編寫新代碼,因爲它將來會被刪除。相反,無論是[MySQLi](http://php.net/manual/en/book.mysqli.php)還是[PDO](http://php.net/manual/en/book.pdo.php)和[是一個更好的PHP開發人員](http://jason.pureconcepts.net/2012/08/better-php-developer/)。 –

回答

0

假設echo($row[$fieldindex]);包含鏈接,你可以做這樣的:

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

將查詢結果從數據庫到<a>標籤的href屬性。

例如:

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

我不知道正是你正在嘗試做的。您的表格是否包含要顯示爲鏈接的網址?然後將它們包裝在<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等問題,並且可以將大部分數據庫交互抽象出來。