如何将数据库输出的文本转换为链接?

时间:2015-06-23 15:51:32

标签: php mysql

Iam class 11th ..最近我开始学习php和mysqliI一直面临着一个问题。我正在尝试创建一个包含一小部分电影的数据库。当页面加载时,它会显示来自数据库的那些电影的列表..但问题是,它将它们显示为一个简单的文本..我希望它们是像链接一样,无论何时点击它都会显示有关该特定电影的信息..但我不想为每部电影编写锚标记链接。还有其他方法吗?

    <?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";


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

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT id, title FROM movies";
$result = $conn->query($sql);

if ($result->num_rows > 0) {

    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["title"].  "<br>";
    }
} else {
    echo "0 results";
}
$conn->close();
?>

2 个答案:

答案 0 :(得分:0)

替换:

$id = $row["id"];
$title = $row["title"];
echo "<a href='/movie/{$id}'>id: {$id} - Name: {$title}</a><br>";

使用:

var newDoc = app.newDoc();

if (newDoc == "undefined")
  app.alert("Undefined NewDoc");

答案 1 :(得分:0)

根据你的代码。

if ($result->num_rows > 0) {

  $htmlLink = '';

  while($row = $result->fetch_assoc()) {
    $id = $row["id"];
    $title = $row["title"];
    $htmlLink .= "<a href='movie.php?{$id}'>id: {$id} - Name: {$title}</a><br>";

    echo $htmlLink;

  }

} else {
  echo "0 results";
}

然后在你的movie.php页面上使用$ _GET []来获取查询字符串数据