如何使数据库结果链接显示为超链接?

问题描述:

我有一个数据库,查询时可以返回有时包含网址的结果。如果结果中有地址,它将始终位于数据库的“位置”字段中。如何使数据库结果链接显示为超链接?

当前,结果返回时,它返回为一行文本(用户必须复制/粘贴到浏览器窗口中)。

我该如何让它返回一个超链接,而不仅仅是一行文本?

这里是我的PHP ...

if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following 

      while($results = mysql_fetch_array($raw_results)){ 
      // $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop 

       echo "<p><strong><u><h2>".$results['SchoolName']."</h2></u></strong>".$results['text']."</p>"; 
       echo "<p><strong>Location:</strong>&nbsp;".$results['SchoolLocation']."".$results['text']."</p><br />"; 
       echo "<p><strong>Notes:</strong>&nbsp;".$results['SchoolNotes']."".$results['text']."</p><br /><br /><br />"; 
       // posts results gotten from database(title and text) you can also show id ($results['id']) 
      } 

     } 
     else{ // if there is no matching rows do following 

      echo "<br /><br /><br /><strong><h2>"."This is great news! The school you searched for is NOT on our Illegitimate High School List..."."</h2></strong>"; 
     } 

    } 
    else{ // if query length is less than minimum 
     echo "Minimum length is ".$min_length; 
    } 
?> 

我做了一些研究,发现this,但我不明白如何将它应用到我的情况。

+4

* PSA:*了'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

你在哪里输出链接? – brbcoding

+0

我明白这一点。但是,在我目前的情况下,我想改进结果的返回方式。我有你的网站,我将开始转换到MySQLi(看起来它会更好地为我)。你能帮助我解决目前的困境吗? – webfrogs

假设从DB你的字段名的位置:

echo "<p><strong>Location:</strong>&nbsp;<a href='".$results['Location']."' target='_blank'>".$results['SchoolLocation']."</a>".$results['text']."</p><br />"; 
+0

取决于如果有或没有http://,考虑在href ='之后立即添加它...... – niklaz

+0

-Nice!这工作。非常感谢你! – webfrogs

+0

追加上面的评论...它工作,但它返回位于该字段中的所有结果作为超链接。我如何调整这个以便“如果”结果包含链接,那么它将被超链接?此外,当我点击结果中的链接时,它只是打开一个空白网页,但就是这样。你能帮我吗? – webfrogs