gpt4 book ai didi

php - 如何在该索引页末尾的 xampp php 中查看数据库中的表?

转载 作者:行者123 更新时间:2023-11-29 17:41:58 26 4
gpt4 key购买 nike

我想在本页末尾查看我的 MySQL 数据库中的表。该表具有属性:“id”、“longurl”和“shorturl”。

<!DOCTYPE html>
<html>
<head>
<title>URL Shortening Service </title>
<head>
<body>
<center>
<h1>URL Shortening Service</h1>
<form method="POST" action="index.php">
Enter your Url: <input type="text" name="urlshort" placeholder="Enter here"> <br><br>
<input type="submit" name="submit">
</form>
</center>
</body>
</html>



<?php

if (isset($_POST["submit"])){
$conn = mysqli_connect('localhost','zarjis','123456','url');




$longurl = $_POST ["urlshort"];
$shorturl = substr(md5( microtime()), rand(0,26),5);
$query = "INSERT INTO shorturl (shorturl,longurl) VALUES ('$shorturl','$longurl')";
$result = mysqli_query($conn,$query);
if ($result) {
echo "Your Short Url Link is :: http://localhost/url/$shorturl ";
}
else{
echo "We are facing some problems";
}
}

if (isset($_GET["link"])) {
$conn = mysqli_connect('localhost','zarjis','123456','url');
$link = $_GET["link"];
$fetchquery = "SELECT * FROM shorturl WHERE shorturl= '$link'";
$fetchresult = mysqli_query($conn,$fetchquery);
while ($row = mysqli_fetch_assoc($fetchresult)) {
$visitlongurl = $row ["longurl"];
header ("Location: $visitlongurl");
exit();
}
}
?>

我想在本页末尾查看数据库中的表。该表位于我的数据库中。

最佳答案

添加此代码,

$con = mysqli_connect('localhost','zarjis','123456','url');
if(mysqli_connect_errno()){
die("Failed to connect to MySQL: " . mysqli_connect_error());
}
$sql = "SELECT * FROM shorturl";
$result = mysqli_query($con, $sql);
echo '<table>';
echo '<tr><th>ID</th><th>Long URL</th><th>Short URL</th></tr>';
while($row = mysqli_fetch_assoc($result)){
echo '<tr><td>'.$row['id'].'</td><td>'.$row['longurl'].'</td><td>'.$row['shorturl'].'</td></tr>';
}
echo '</table>';

关于php - 如何在该索引页末尾的 xampp php 中查看数据库中的表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49956152/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com