PHP Show how many players online

Results 1 to 16 of 16
  1. #1
    The Sky's the Limit ^_^ GzFKoron is offline
    MemberRank
    Jul 2010 Join Date
    1,118Posts

    PHP Show how many players online

    I am in the middle of adding PHP functions to my website for GunZ, and I am trying to figure out the players online part. I just want it to show '$players are currently online'

    I tried this:
    PHP Code:
    <?php
                    $players 
    "SELECT CurrPlayer Opened FROM ServerStatus')";
                    
    ?>
    But nothing showed up, do I then need to add a certain part of code to the file outside of the PHP code?

    I have tried looking at other server status releases here but with no luck into it answering my question.


  2. #2

    Re: PHP Show how many players online

    Just did this earlier, not sure if there are any syntax errors though. Feel free to test.

    PHP Code:
    <?php

    $query 
    mssql_query ("SELECT CurrPlayer FROM ServerStatus WHERE Opened = 1");

    if (
    mssql_num_rows($query) == 0)
    {
        echo 
    "There are no servers available.";
        return;
    }

    $totalplayers 0;

    while (
    $fetch mssql_fetch_assoc($query))
    {
        
    $totalplayers += $fetch[CurrPlayer];
    }

    echo 
    "There are "$totalplayers ." players online.";

    ?>

  3. #3
    Account Upgraded | Title Enabled! alfredao is offline
    MemberRank
    Jan 2008 Join Date
    Coronel FabriciLocation
    705Posts

    Re: PHP Show how many players online

    PHP Code:

    $players 
    0;
    $q mssql_query("SELECT CurrPlayer FROM ServerStatus WHERE Opened = 1");
    while(
    $b mssql_fetch_array($q))
    {
           
    $players += $b['CurrPlayer'];
    }
    echo 
    $players
    EDIT: lol posted together.
    Last edited by alfredao; 24-04-11 at 09:45 AM.

  4. #4
    Ecchi addicted adz28 is offline
    MemberRank
    Nov 2008 Join Date
    IkebukuroLocation
    524Posts

    Re: PHP Show how many players online

    Not tested but is suposed to work.
    PHP Code:
    <?php
    servercount 
    $servercount $srv['CurrPlayer']
    ?>
    and for displaying in text
    PHP Code:
    <?=$servercount?> Players online.

  5. #5
    The Sky's the Limit ^_^ GzFKoron is offline
    MemberRank
    Jul 2010 Join Date
    1,118Posts

    Re: PHP Show how many players online

    Quote Originally Posted by Linear88 View Post
    Just did this earlier, not sure if there are any syntax errors though. Feel free to test.

    PHP Code:
    <?php

    $query 
    mssql_query ("SELECT CurrPlayer FROM ServerStatus WHERE Opened = 1");

    if (
    mssql_num_rows($query) == 0)
    {
        echo 
    "There are no servers available.";
        return;
    }

    $totalplayers 0;

    while (
    $fetch mssql_fetch_assoc($query))
    {
        
    $totalplayers += $fetch[CurrPlayer];
    }

    echo 
    "There are "$totalplayers ." players online.";

    ?>
    Thanks, just tested it and this is the error that shows:

    Fatal error: Call to undefined function mssql_query() in C:\xampp\htdocs\index.php on line 45

    And on this line is:
    PHP Code:
    $query mssql_query ("SELECT CurrPlayer FROM ServerStatus WHERE Opened = 1"); 

  6. #6
    Pee Aitch Pee Dave is offline
    MemberRank
    Mar 2011 Join Date
    The NetherlandsLocation
    722Posts

    Re: PHP Show how many players online

    Quote Originally Posted by SheenUK View Post
    Thanks, just tested it and this is the error that shows:

    Fatal error: Call to undefined function mssql_query() in C:\xampp\htdocs\index.php on line 45

    And on this line is:
    PHP Code:
    $query mssql_query ("SELECT CurrPlayer FROM ServerStatus WHERE Opened = 1"); 
    Enable the php_mssql.dll extension in your php.ini. (Remove the ; in front of the line.)
    After that, restart your Apache.

  7. #7
    The Sky's the Limit ^_^ GzFKoron is offline
    MemberRank
    Jul 2010 Join Date
    1,118Posts

    Re: PHP Show how many players online

    Quote Originally Posted by adz28 View Post
    Not tested but is suposed to work.
    PHP Code:
    <?php
    servercount 
    $servercount $srv['CurrPlayer']
    ?>
    and for displaying in text
    PHP Code:
    <?=$servercount?> Players online.
    Parse error: syntax error, unexpected '=' in C:\xampp\htdocs\index.php on line 45

    This is the error from this.

    I will just show you my website code:

    Code:
    <?php
    session_start();
    include("secure/config.php");
    include("secure/functions.php");
    include("secure/anti_sql.php");
    include("language/$language.php");
    ?>
    
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    	<head>
    		<title>Games Anniversary</title>
    		
    		<!-- meta tags -->
    		<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    		<meta http-equiv="X-UA-Compatible" content="ie=edge,chrome=1"/> 
    		
    		<!-- styling -->
    		<link href="css/style.css" rel="stylesheet" type="text/css"/>
    		
    		<!-- scripts -->
    	</head>
    
    	<body>
    		<div id="header_repeat">
    			<div id="header">
    				<img src="http://forum.ragezone.com/images/header.png" />
    			</div><!-- /header -->
    		</div><!-- /header_repeat -->
    		
    		<div id="nav_bg">
    			<div id="nav">
    				<ul id="navigation">
    					<li><a href="index.html"><img src="http://forum.ragezone.com/images/nav/home.png" alt=""/></a></li>
    					<li><a href="http://europegunz.co.uk/register.php"><img src="http://forum.ragezone.com/images/nav/register.png" alt=""/></a></li>
    					<li><a href="#"><img src="http://forum.ragezone.com/images/nav/download.png" alt=""/></a></li>
    					<li><a href="http://forum.europe-gunz.eu"><img src="http://forum.ragezone.com/images/nav/forum.png" alt=""/></a></li>
    					<li><a href="#"><img src="http://forum.ragezone.com/images/nav/ranking.png" alt=""/></a></li>
    					<li><a href="#"><img src="http://forum.ragezone.com/images/nav/item_shop.png" alt=""/></a></li>
    				</ul><!-- /navigation -->
    				
    				<div id="players_online">
    				<?php
    				servercount = $servercount + $srv['CurrPlayer']
    				?>
    				<?=$servercount?> Players online.
    				
    				</div><!-- /players_online -->
    			</div><!-- /nav -->
    		</div><!-- /nav_bg -->
    		
    		<div id="wrap">
    			<div id="sidebar">
    				<div id="sidebar_mid">
    					<div id="login_box">
    						<div class="divider"></div><!-- divider -->
    					</div><!-- /login_box -->
    					
    					<div class="box">
    						<div class="title">
    							<p>&raquo; Advertisement</p>
    						</div><!-- /title -->
    						
    						<div class="ctext">
    							<img src="ad-1.png" />
    							
    							<div class="divider" style="margin: 0px;"></div><!-- divider -->
    						</div><!-- /ctext -->
    					</div><!-- /box -->
    					
    					<div class="box">
    						<div class="title">
    							<p>&raquo; Official Sponsor</p>
    						</div><!-- /title -->
    						
    						<div class="ctext">
    							<img src="sp-1.png" />
    							
    							<div class="divider" style="margin: 0px;"></div><!-- divider -->
    						</div><!-- /ctext -->
    					</div><!-- /box -->
    					
    					<div id="donate-img">
    						<a href="#"></a>
    					</div><!-- /donate-img -->
    					
    					<div id="teamspeak-img">
    						<a href="#"></a>
    					</div><!-- /donate-img -->
    					
    					<div id="game-img">
    						<a href="#"></a>
    					</div><!-- /donate-img -->
    					
    					<div class="clear"></div><!-- clear -->
    				</div><!-- /sidebar_mid -->
    				
    				<div id="sidebar_bot"></div>
    			</div><!-- /sidebar -->
    					
    			<div id="content">
    				<div id="banners">
    					<div id="download">
    						<a href="#"></a>
    					</div><!-- /download -->
    					
    					<div id="user_cp">
    					
    					</div><!-- /user_cp -->
    				</div><!-- /banners -->
    				
    				<div class="clear"></div><!-- clear -->
    				
    				<div id="slider">
    					<script>var showfoldername='';</script><script charset="utf-8" src="show.js" type="text/javascript"></script>
    				</div><!-- /slider -->
    				
    				<div id="base_bottom">
    					<div id="items">
    						<h1>Latest Items</h1>
    						<div class="divider"></div><!-- divider -->
    						
    						<div style="float: left; width: 140px;">
    							<div class="item">
    								<img src="http://forum.ragezone.com/images/items/sample.png" align="left" alt=""/>
    								<p><strong>Item #1</strong></p>
    								<p>Type: Special</p>
    								<p>Sex: All</p>
    								<p>Level: 15</p>
    								<p>Price: G1111</p>
    							</div><!-- /item -->
    							
    							<div class="item">
    								<img src="http://forum.ragezone.com/images/items/sample.png" align="left" alt=""/>
    								<p><strong>Item #1</strong></p>
    								<p>Type: Special</p>
    								<p>Sex: All</p>
    								<p>Level: 15</p>
    								<p>Price: G1111</p>
    							</div><!-- /item -->
    							
    							<div class="item">
    								<img src="http://forum.ragezone.com/images/items/sample.png" align="left" alt=""/>
    								<p><strong>Item #1</strong></p>
    								<p>Type: Special</p>
    								<p>Sex: All</p>
    								<p>Level: 15</p>
    								<p>Price: G1111</p>
    							</div><!-- /item -->
    						</div>
    						
    						<div style="float: right; width: 140px;">
    							<div class="item">
    								<img src="http://forum.ragezone.com/images/items/sample.png" align="left" alt=""/>
    								<p><strong>Item #1</strong></p>
    								<p>Type: Special</p>
    								<p>Sex: All</p>
    								<p>Level: 15</p>
    								<p>Price: G1111</p>
    							</div><!-- /item -->
    							
    							<div class="item">
    								<img src="http://forum.ragezone.com/images/items/sample.png" align="left" alt=""/>
    								<p><strong>Item #1</strong></p>
    								<p>Type: Special</p>
    								<p>Sex: All</p>
    								<p>Level: 15</p>
    								<p>Price: G1111</p>
    							</div><!-- /item -->
    							
    							<div class="item">
    								<img src="http://forum.ragezone.com/images/items/sample.png" align="left" alt=""/>
    								<p><strong>Item #1</strong></p>
    								<p>Type: Special</p>
    								<p>Sex: All</p>
    								<p>Level: 15</p>
    								<p>Price: G1111</p>
    							</div><!-- /item -->
    						</div>
    						
    						<div class="clear"></div><!-- clear -->
    					</div><!-- /items -->
    					
    					<div id="news">
    						<h1>News</h1>
    						<div class="divider"></div><!-- divider -->
    						
    						<p>- <a href="#">- No news to display -</a></p>
    						
    						<div class="clear1"></div><!-- clear1 -->
    						
    						<h1>Events</h1>
    						<div class="divider"></div><!-- divider -->
    						
    						<p>- <a href="#">- No active Events -</a></p>
    						
    						<p class="hot"><a>HOT!</a> Items available!</p>
    					</div><!-- /news -->
    				</div><!-- /base_bottom -->
    			</div><!-- /content -->
    			
    			<div class="clear" style="margin-top: 30px;"></div><!-- clear -->
    			
    			<div id="footer">
    				<img src="http://forum.ragezone.com/images/tm_footer.png" alt="" align="left"/>
    				<div id="copyright">&copy;2011 Europe GunZ.</div> All Rights Reserved.
    				<p><a href="#">Home</a> | <a href="http://forum.europe-gunz.eu">Forums</a> | <a href="#">Privacy Policy</a> | <a href="#">Download</a></p>
    				
    				<div class="clear"></div><!-- clear -->
    			</div><!-- /footer -->
    		</div><!-- /wrap -->
    	</body>
    </html>
    config.php
    Code:
    $host = "*********\SQLEXPRESS";
    $user = "**";
    $pass = "*******";
    $dbname = "GunzDB";
    
    $connect = odbc_connect("Driver={SQL Server};Server={$host}; Database={$dbname}", $user, $pass) or die("Can't connect the MSSQL server.");
    Quote Originally Posted by SuperWaffle View Post
    Enable the php_mssql.dll extension in your php.ini. (Remove the ; in front of the line.)
    After that, restart your Apache.
    I did this when I setup xampp.
    Last edited by GzFKoron; 24-04-11 at 09:54 AM.

  8. #8
    Pee Aitch Pee Dave is offline
    MemberRank
    Mar 2011 Join Date
    The NetherlandsLocation
    722Posts

    Re: PHP Show how many players online

    Config for MSSQL: (Put this in your config.php)

    PHP Code:
    <?php
    $host 
    "BLABLA\SQLEXPRESS";
    $user "";
    $pass "";
    $dbn  "GunzDB";

    $con mssql_connect($host$user$pass) or die("Failed to connect!");
    mssql_select_db($dbn$con) or die("Failed to select the database!");
    ?>
    Yes, the user and password has to be empty for MSSQL. (At least, it has to be empty to work on my host.)
    Last edited by Dave; 24-04-11 at 10:02 AM.

  9. #9
    Ecchi addicted adz28 is offline
    MemberRank
    Nov 2008 Join Date
    IkebukuroLocation
    524Posts

    Re: PHP Show how many players online

    Ok i tested this so i know it works :).
    PHP Code:
    <?php
    $q 
    mssql_query("SELECT * FROM ServerStatus");
        
    $r mssql_fetch_object($q);
        
    $users "<font color='#FFFFFF'>".$r->CurrPlayer."/".$r->MaxPlayer."</font>";
    ?>
    Text displayed
    PHP Code:
    Players Online: <?=$users?>
    It takes data from currplayer column in account and maxplayer column, like executing
    Code:
    SELECT CurrPlayer FROM ServerStatus
    SELECT MaxPlayer FROM ServerStatus
    so it works <.<

  10. #10
    The Sky's the Limit ^_^ GzFKoron is offline
    MemberRank
    Jul 2010 Join Date
    1,118Posts

    Re: PHP Show how many players online

    Quote Originally Posted by SuperWaffle View Post
    Config for MSSQL: (Put this in your config.php)

    PHP Code:
    <?php
    $host 
    "BLABLA\SQLEXPRESS";
    $user "";
    $pass "";
    $dbn  "GunzDB";

    $con mssql_connect($host$user$pass) or die("Failed to connect!");
    mssql_select_db($dbn$con) or die("Failed to select the database!");
    ?>
    Yes, the user and password has to be empty for MSSQL. (At least, it has to be empty to work on my host.)
    Error: Fatal error: Call to undefined function mssql_connect() in C:\xampp\htdocs\secure\config.php on line 41

    Quote Originally Posted by adz28 View Post
    Ok i tested this so i know it works :).
    PHP Code:
    <?php
    $q 
    mssql_query("SELECT * FROM ServerStatus");
        
    $r mssql_fetch_object($q);
        
    $users "<font color='#FFFFFF'>".$r->CurrPlayer."/".$r->MaxPlayer."</font>";
    ?>
    Text displayed
    PHP Code:
    Players Online: <?=$users?>
    It takes data from currplayer column in account and maxplayer column, like executing
    Code:
    SELECT CurrPlayer FROM ServerStatus
    SELECT MaxPlayer FROM ServerStatus
    so it works <.<
    Error: Fatal error: Call to undefined function mssql_query() in C:\xampp\htdocs\index.php on line 45

  11. #11
    Ecchi addicted adz28 is offline
    MemberRank
    Nov 2008 Join Date
    IkebukuroLocation
    524Posts

    Re: PHP Show how many players online

    make this a new php script named "currpl.php"
    PHP Code:
    <?php
    $q 
    mssql_query("SELECT * FROM ServerStatus");
        
    $r mssql_fetch_object($q);
        
    $users "<font color='#FFFFFF'>".$r->CurrPlayer."/".$r->MaxPlayer."</font>";
    ?>
    <br />
    <table>

    <tr>
    <td>
    <font color="#FFFFFF">
    Players Online:</font></td><td> <?=$users?></td>
    </tr>
    </table>
    and in the part of the index you want to make visible the current players, just add this
    PHP Code:
    <?php include "currpl.php"?>
    in a table or div frame or w.e

  12. #12
    The Sky's the Limit ^_^ GzFKoron is offline
    MemberRank
    Jul 2010 Join Date
    1,118Posts

    Re: PHP Show how many players online

    Fatal error: Call to undefined function mssql_query() in C:\xampp\htdocs\currpl.php on line 2

    Its kinda odd, there seems to be something wrong with the configuration yet ive removed the ; from php_mssql.dll in php.ini so what could it possibly be?

  13. #13
    Account Upgraded | Title Enabled! alfredao is offline
    MemberRank
    Jan 2008 Join Date
    Coronel FabriciLocation
    705Posts

    Re: PHP Show how many players online


  14. #14
    The Sky's the Limit ^_^ GzFKoron is offline
    MemberRank
    Jul 2010 Join Date
    1,118Posts

    Re: PHP Show how many players online

    Quote Originally Posted by alfredao View Post
    Yep i've done that, still the same error. >.>

  15. #15
    Aristrum Mark is offline
    MemberRank
    Aug 2007 Join Date
    United KingdomLocation
    474Posts

    Re: PHP Show how many players online

    SheenUK, are you using XAMPP by chance? They removed support for MSSQL in 1.7.4, you'll have to get version 1.7.3 from sourceforge. Then do the php.ini/dll stuff if it's not working :)

  16. #16
    The Sky's the Limit ^_^ GzFKoron is offline
    MemberRank
    Jul 2010 Join Date
    1,118Posts

    Re: PHP Show how many players online

    Quote Originally Posted by Aristrum View Post
    SheenUK, are you using XAMPP by chance? They removed support for MSSQL in 1.7.4, you'll have to get version 1.7.3 from sourceforge. Then do the php.ini/dll stuff if it's not working :)
    Yes I am using xampp and thanks Aristrum, didn't think xampp would stoop so low >.>



Advertisement