注意:使用未定义的常量database_name - 在第97行的C:\ xampp \ htdocs \ visitor \ index.php中假定为'database_name'

时间:2017-09-10 13:50:09

标签: javascript php jquery mysql

请帮助我;我正在制作一个页面,通过使用PHP,JAVASCRIPTS MSQL,能够通过他们的IP地址看到网站的访问者,但是当我尝试执行下面的代码时,我遇到了很多错误。我不知道这里有什么问题。请帮忙! 以下是我得到的错误:

  

注意:使用未定义的常量database_name - 假设   第97行的C:\ xampp \ htdocs \ visitor \ index.php中的“database_name”

     

警告:mysql_num_rows()要求参数1为resource,boolean   在第105行的C:\ xampp \ htdocs \ visitor \ index.php中给出

     

警告:mysql_num_rows()要求参数1为resource,boolean   在第119行的C:\ xampp \ htdocs \ visitor \ index.php中给出

     

警告:mysql_num_rows()要求参数1为resource,boolean   在第137行的C:\ xampp \ htdocs \ visitor \ index.php中给出

     

警告:mysql_num_rows()要求参数1为resource,boolean   在第144行的C:\ xampp \ htdocs \ visitor \ index.php中给出

请帮助!

<!DOCTYPE html>  
<head>
    <title>VISITOR COUNTER</title>
    <style>
        *{margin: 0px;
            padding: 0px;
        }

        body{text-align: center;
            width: 66%;
            margin: auto;
            /*background: url(images/background.jpg);*/
            background-color: green;
            font-family: Helvetica, Arial, sans-serif;
        }

        h1{
            font-family: Helvetica, Arial, sans-serif;
            text-align: center;
            font-size: 60px;
            margin-top: 80px;
            color: #000;
            text-shadow: 2px 2px 0px rgba(255,255,255,.7), 5px 7px 0px rgba(0,0,0,0.1);
        }

        .container{height: 250px;
            width: 31%;
            float: left;
            margin-top: 40px;
        }

        .round{
            width: 160px;
            height: 160px;
            border-radius: 50%;
            background: #fff;
            margin: 20px auto;
        }

        .values{
            color: #888;
            line-height: 3.8em;
            font-size: 40px;
        }

        p{
            font-size: 32px;
            color: #fff;
            font-weight: bold;
        }


    </style>

    <script type="text/javascript" src="js/jquery.min.js" ></script>
      <script src="js/jquery-3.2.1.min.js"></script>
  <script src="js/jquery-2.2.4.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            setInterval(function()
            {
                $.ajax
                ({
                    type: 'post',
                    url:'',
                    data:{
                        get_online_visitor:"online_visitor",

                    },
                    success:function(response){
                        if(response!="")
                        {
                            $("#online_visitor_val").html(response);
                        }

                    }

                });
            }, 10000)
        });
    </script>

</head>
<body>

<?php
session_start();
$_SESSION['session']=session_id();

$host="localhost";
$username="root";
$password="";
$databasename="visitor";
$connect=mysql_connect($host, $username, $password);
$db=mysql_select_db(database_name);

function total_online()
{
    $current_time=time();
    $timeout = $current_time - (60);

    $session_exist = mysql_query("SELECCT session FROM total_visitors WHERE session='".$_SESSION['session']."'");
    $session_check = mysql_num_rows($session_exist);

    if($session_check==0 && $_SESSION['session']!="")
    {
        mysql_query("INSERT INTO total_visitors values ('', '".$_SESSION['session']."','".$current_time."')");

    }
    else
    {
        $sql = mysql_query("UPDATE total_visitors SET time='".time()."' WHERE session='".$_SESSION['session']."'");

    }

    $select_total = mysql_query("SELECCT * FROM total_visitors WHERE time>= '$timeout'");
    $total_online_visitors = mysql_num_rows($select_total);
    return $total_online_visitors;
}

if(isset($_POST['get_online_visitor']))
{
    $total_online=total_online();
    echo $total_online;
    exit();
}
?>

<?php
//To get total online visitors
$total_online_visitors=total_online();

//To get total visitors
$total_visitors = mysql_query("SELECT * FROM total_visitors");
$total_visitors = mysql_num_rows($total_visitors);

//To insert page view and select total page view
$user_ip=$_SERVER['REMOTE_ADDR'];
$page=$_SERVER['PHP_SELF'];
mysql_query("insert into pageviews values('','$page','$user_ip')");
$pageviews = mysql_query("SELECT * FROM pageviews");
$total_pageviews = mysql_num_rows($pageviews);
?>

<div class="container">
    <div class="round"><p class="values"><?php echo $total_visitors;?></p></div>
    <p>Total Visitors</p>

</div>

<div class="container">
    <div class="round"><p class="values" id="online_visitor_val"><?php echo $total_online_visitors;?></p></div>
    <p>Online Visitors</p>

</div>

<div class="container">
    <div class="round"><p class="values"><?php echo $total_pageviews;?></p></div>
    <p>Total Page Views</p>

</div>


</body>
</html>

2 个答案:

答案 0 :(得分:0)

$databasename="visitor";
$connect=mysql_connect($host, $username, $password);
$db=mysql_select_db(database_name);

最后一行应该是

$db=mysql_select_db($databasename);

至于这部分,你打字单词SELECT,所以需要纠正。

$select_total = mysql_query("SELECCT * FROM total_visitors WHERE time>= '$timeout'");

答案 1 :(得分:-1)

在您的代码中,您没有使用php变量分配正确的数据库名称

您的代码

$db=mysql_select_db(database_name);

更正代码

$db=mysql_select_db($databasename);

您可以使用SQL

检查php中的mysql_error ()错误
 $sql ="SELECT session FROM total_visitors WHERE session='".$_SESSION['session']."'";

 echo $sql; 

/* Check SQL in phpmyadmin*/

$session_exist = mysql_query($sql) or die(mysql_error ());`