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

时间:2018-04-21 13:16:45

标签: php mysql mysqli xampp

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

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

1 个答案:

答案 0 :(得分:0)

添加此代码,

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