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条)

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?