2014-12-06 66 views
1

我想制作一个从服务器上的MySQL数据库表(www.domain.com)获取结果的PHP页面。动态地从分表中的URL字符串链接到HTML/PHP页面

该表具有以下字段:

 
ID (ie: 1) 
Date (ie: 2014-12-06) 
Text (ie: Guns 'n Roses) 
URL (ie: https://www.youtube.com/watch?v=xxxxxxx) 
Link (ie: Watch on YouTube) 

URL在文本链接到网页锚定。

我的查询很简单直接:简单的select * from table。当运行在屏幕上,PHP页打印下表:

2014-12-06 --- Guns 'n Roses --- Watch on YouTube 

,其中,通过点击“在YouTube上观看”,实际地址就变成了:

www.domain.com/https://www.youtube.com/watch?v=xxxxxxx

我怎样才能摆脱服务器的根,并有一个工作链接?

的代码是:

<!DOCTYPE html> 
<html> 

<head> 
<meta charset="utf-8"> 
</head> 

<body> 

<div id="youtube"> 

<?php 

require "parameters.php"; 

$con = mysqli_connect($host,$user,$password,$db); 

if (mysqli_connect_errno()) { 
echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 

$result = mysqli_query($con,"SELECT Date, Text, URL, Link FROM $db.$table_10 ORDER BY date desc"); 

echo "<table>"; 
echo "<tr><th>Date</th><th>Text</th><th>Link</th></tr>"; 
echo "<tr><td></br></td></tr>"; 


while($row = mysqli_fetch_array($result)) { 
$safe_url = mysqli_real_escape_string($con, $row['URL']); 
echo "<tr>"; 
echo "<td>" . $row['Date'] . "&#160;&#160;</td>"; 
echo "<td>" . $row['Text'] . "</td>"; 
echo "<td><a href=" . $safe_url . " " . "target="."_blank".">" . $row['Link'] . "</a></td>"; 
echo "</tr>"; 
} 
echo "</table>"; 

mysqli_close($con); 
?> 

</div> 

</body> 
</html> 

感谢您的回复和帮助我!

+0

请准确显示转储'SELECT'结果的代码。 – angelsl 2014-12-06 17:40:05

+0

您需要向我们展示您的PHP/HTML如何检索数据。这不是关于“如何摆脱”,而是关于使用正确的语法。 – 2014-12-06 17:41:03

+1

在我看来,问题标题是错误的 – vladkras 2014-12-06 17:43:28

回答

3

如果一个链接没有以协议作为前缀,它将变成相对于包含该链接的页面。

<a href="example.com/somepage">Webpage</a> 

点击后变成http://example.net/example.com/somepage

<a href="http://example.com/somepage">Webpage</a> 

变得HTTP:以http //example.com/somepage

当你显示的链接,它的前缀://

-

这是一个JSFiddle显示这个,http://jsfiddle.net/7fnpg5o1/

+0

我确信他有https,认真看他的例子 – vladkras 2014-12-06 17:46:55

+0

是的,URLs前缀正确 – Wailmanager 2014-12-07 09:53:47