第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 ?在網(wǎng)址中?

如何使用 ?在網(wǎng)址中?

PHP
搖曳的薔薇 2023-08-19 14:28:40
我正在嘗試找出一種方法,讓一個 PHP 頁面顯示我的所有博客文章,但讓 URL 決定從該數(shù)據(jù)庫請求什么文章。有點像這樣:localhost/bolg/posts.php?pid=1在我的數(shù)據(jù)庫中,我將其設(shè)置為每個帖子都有一個與之關(guān)聯(lián)的 ID。所以我想要的是將 pid=1 并將其放入 MySQL 代碼中。這是 post.php 的 PHP 代碼<?php$servername = "localhost";$username = "root";$password = "";$dbname = "test";// Create connection$conn = new mysqli($servername, $username, $password, $dbname);// Check connectionif ($conn->connect_error) {    die("Connection failed: " . $conn->connect_error);}$sql = "SELECT id, title, content, date FROM posts where id =3";$result = $conn->query($sql);if ($result->num_rows > 0) {    // output data of each row    while($row = $result->fetch_assoc()) {        echo "<h1> ". $row["title"]. "</h1>". $row["content"]. "" . $row["date"] . "<br>";    }} else {    echo "0 results";}$conn->close();?>
查看完整描述

1 回答

?
藍(lán)山帝景

TA貢獻(xiàn)1843條經(jīng)驗 獲得超7個贊

example.com?pid=10假設(shè)您在瀏覽器地址欄中輸入,您可以pid使用$_GET數(shù)組捕獲該變量,當(dāng)使用查詢字符串調(diào)用頁面時,PHP 會自動為您填充該變量。


使用現(xiàn)有代碼作為起點,您可以


<?php


mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

$servername = "localhost";

$username = "root";

$password = "";

$dbname = "test";


if (isset($_GET['pid'])) {

? ? // Create connection

? ? $conn = new mysqli($servername, $username, $password, $dbname);

? ??

? ? $sql = "SELECT id, title, content, date FROM posts where id = ?";

? ? $stmt = $conn->prepare($sql);

? ? $stmt->bind_param('i', $_GET['pid']);

? ? $stmt->execute();


? ? $result = $stmt->get_result();


? ? if ($result->num_rows > 0) {

? ? ? ? // output data of each row

? ? ? ? // while looop is not necessary, you are only returning one row

? ? ? ? $row = $result->fetch_assoc();

? ? ? ? echo "<h1> ". $row["title"]. "</h1>". $row["content"]. "" . $row["date"] . "<br>";

? ? }

? ? $conn->close();

} else {

? ? echo "0 results";

}



查看完整回答
反對 回復(fù) 2023-08-19
  • 1 回答
  • 0 關(guān)注
  • 122 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號