dongyinpan9284 2016-02-01 11:55
浏览 18
已采纳

从MySQL DB获取数据后如何在我的php页面中创建可点击的URL?

I'm trying to pull data from 1 column of a db and making it into a link where the URL is being pulled from another column. Unfortunately, I have not been able to do this. I've attached the screenshot of the output here's the code I'm using:

$sql = "SELECT DISTINCT item_title, item_url, item_date, item_author FROM nbth";
 $result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
    echo "<a>" . $row["item_title"]. " url: " . $row["item_url"]. " <br> - By " . $row["item_author"] . " " . $row["item_date"]. "<br><br></a>" ;
    $HTML .= "<a href=".$row['item_url'].">".$row['item_title']."</a>";
    echo $HTML;
}
} else {
echo "0 results";
}

Screenshot of the results: [![enter image description here][1]][1]

The titles do become links but the get looped again and again, the first title in the first line, then next the first title and second, then the first, second and third, so on and so forth. So, how do I fix this looping and still display the titles as links.

  • 写回答

2条回答 默认 最新

  • doumeng06063991 2016-02-01 12:03
    关注
    $HTML .= "<a href=".$row['item_url'].">".$row['item_title']."</a>";
    

    Remove the period (string concat) operator before the equal sign:

    $HTML = "<a href=".$row['item_url'].">".$row['item_title']."</a>";
    

    It causes each iteration of the loop to concat the "<a href=".$row['item_url'].">".$row['item_title']."</a>"; part every time to the $HTML variable.

    Also, pls enclose the href attribute's value by quotation marks(").

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料