如何在xampp php的endofthis index页中查看数据库中的表?

1hdlvixo  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(310)

我想在本页末尾查看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();
    }
}
?>

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

wgeznvg7

wgeznvg71#

添加此代码,

$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>';

相关问题