PHP+JS随机访问数据库友链

可用于博客友情链接访问

点击查看代码
<?php

$servername = "localhost";

$username = "数据库用户名";

$password = "数据库密码";

$dbname = "数据库名字";

 

// 创建连接

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

// Check connection

if ($conn->connect_error) {

    die("连接失败: " . $conn->connect_error);

} 

 

$sql = "SELECT url FROM typecho_links";//数据表

$result = $conn->query($sql);

?>

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>随机跳转</title>

</head>

<body>

        <script>

    function jumpUrl(){

        var arr = new Array(

            <?php

              while($row = $result->fetch_assoc()) {//遍历数组

                   print('"'.$row['url'].'",');//输出数据表内的链接

                }

                    $conn->close();

            ?>

        );

     window.location.href = arr[Math.floor(Math.random() * arr.length)];

    }

    

    setTimeout(function () {

    jumpUrl();},6000);//设定跳转时间,1s=1000

    </script>

    </body>

</html>

posted @ 2021-11-24 21:05  小kBlog  阅读(150)  评论(0编辑  收藏  举报