Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从mysql中提取链接并使其可点击?_Php_Html_Mysql_Sql - Fatal编程技术网

Php 从mysql中提取链接并使其可点击?

Php 从mysql中提取链接并使其可点击?,php,html,mysql,sql,Php,Html,Mysql,Sql,我有一个存储URL的数据库表。我需要的是从表中抓取这些URL,并使其能够以URL的标题作为锚 这就是我尝试过的: while($row4 = mysql_fetch_assoc($result4)) { echo "<a href =\"$row4[Url1]\">".$row4['Title1']. "</a>"; } while($row4=mysql\u fetch\u assoc($result4)) { 回声“; } 例如,它显示mytilte1即yo

我有一个存储URL的数据库表。我需要的是从表中抓取这些URL,并使其能够以URL的标题作为锚

这就是我尝试过的:

while($row4 = mysql_fetch_assoc($result4))
{
   echo "<a href =\"$row4[Url1]\">".$row4['Title1']. "</a>";
}
while($row4=mysql\u fetch\u assoc($result4))
{
回声“;
}
例如,它显示my
tilte1
youtube
Url1
www.youtube.com

但当我点击它时,它将进入
localhost/mysite/www.youtube.com

如何解决此问题?

请尝试:

echo "<a href =\"http://$row4[Url1]\">".$row4['Title1']. "</a>";
echo”“;

在链接前面添加
http://
。然后它会到达你想要的地方。

你需要在前面加上http://

echo '<a href ="http://'.$row4['Url1'].'">'.$row4['Title1']. '</a>';
echo';

能否检查您的url 1字段是否为正确的url?查看url中是否有http://协议。如果没有,则需要将其添加到表中,或者以编程方式将http://协议添加到链接中

此外,您还可以使用以下代码点火器框架形式的函数。它为url准备您的链接。准备url($row4[Url1]),而不仅仅是$row4[Url1]

function prep_url($str = '')
{
  if ($str == 'http://' OR $str == '')
  {
    return '';
  }
  $url = parse_url($str);
  if ( ! $url OR ! isset($url['scheme']))
  {
    $str = 'http://'.$str;
  }
  return $str;
}
试试这个

while($row4 = mysql_fetch_assoc($result4))
{
   echo "<a href ='http://".$row4['Url1']."'>".$row4['Title1']. "</a>";
}
while($row4=mysql\u fetch\u assoc($result4))
{
回声“;
}

您应该从中创建一个绝对链接,并且不要忘记在引号中添加属性值。 我建议:

echo '<a href="http://www.'.$row4[Url1].'">'.$row4['Title1']. '</a>';
//by doing this you also won't need any of \ slashes
echo';
//通过这样做,您也不需要任何\斜杠

我输入用引号括起来的URL,例如: "" 然后我使用:

.$row['date'].““$row['title']”.“

结果是一个可点击的链接,形式如下:
移除<和a之间的空间,(我必须为代码添加一个空间来发布。

请在
www.youtube.com
之前将
http://
放入您的数据库中,然后在您的示例中进行更改my
tilte1
youtube
Url1
http://www.youtube.com
。如果它对您有帮助,请接受它。这不会提供问题的答案n、 若要评论或要求作者澄清,请在其帖子下方留下评论。@AlmaDoMundo它如何不提供问题的答案?您的意思是,如果链接是https或ftp,或者其他链接怎么办?