PHP Error on line

Thread Solved

Join Date: Oct 2009
Posts: 9
Reputation: foxwizzy is an unknown quantity at this point 
Solved Threads: 1
foxwizzy foxwizzy is offline Offline
Newbie Poster

PHP Error on line

 
0
  #1
Oct 31st, 2009
hello all i am new to this forum and need some help whit php.
I am trying to create a script that shows the number of users online.

  1. <?php
  2.  
  3. $host = "xx.xxx.xxx.xx";
  4. $user = "xxxx";
  5. $pass = "xxxx";
  6. $db = "users"
  7.  
  8. $conn = mysql_connect("$host", "$user", "$pass") or die ("Unable to connect to database.");
  9. mysql_select_db("$db", $conn);
  10.  
  11.  
  12. include_once ("usersOnline.class.php");
  13. $visitors_online = new usersOnline();
  14.  
  15. if (count($visitors_online->error) == 0) {
  16.  
  17. if ($visitors_online->count_users() == 1) {
  18. echo "There is " . $visitors_online->count_users() . " visitor online";
  19. }
  20. else {
  21. echo "There are " . $visitors_online->count_users() . " visitors online";
  22. }
  23. }
  24. else {
  25. echo "<b>Users online class errors:</b><br /><ul>\r\n";
  26. for ($i = 0; $i < count($visitors_online->error); $i ++ ) {
  27. echo "<li>" . $visitors_online->error[$i] . "</li>\r\n";
  28. }
  29. echo "</ul>\r\n";
  30.  
  31. }
  32. ?>
My problem is : Parse error: parse error in C:\wamp\www\Untitled-2.php on line 24

My line 24 is this
  1. $conn = mysql_connect("$host", "$user", "$pass") or die ("Unable to connect to database.");
  2. mysql_select_db("$db", $conn);

This is my usersOnline.class.php
  1. class usersOnline {
  2.  
  3. var $timeout = 600;
  4. var $count = 0;
  5. var $error;
  6. var $i = 0;
  7.  
  8. function usersOnline () {
  9. $this->timestamp = time();
  10. $this->ip = $this->ipCheck();
  11. $this->new_user();
  12. $this->delete_user();
  13. $this->count_users();
  14. }
  15.  
  16. function ipCheck() {
  17. if (getenv('HTTP_CLIENT_IP')) {
  18. $ip = getenv('HTTP_CLIENT_IP');
  19. }
  20. elseif (getenv('HTTP_X_FORWARDED_FOR')) {
  21. $ip = getenv('HTTP_X_FORWARDED_FOR');
  22. }
  23. elseif (getenv('HTTP_X_FORWARDED')) {
  24. $ip = getenv('HTTP_X_FORWARDED');
  25. }
  26. elseif (getenv('HTTP_FORWARDED_FOR')) {
  27. $ip = getenv('HTTP_FORWARDED_FOR');
  28. }
  29. elseif (getenv('HTTP_FORWARDED')) {
  30. $ip = getenv('HTTP_FORWARDED');
  31. }
  32. else {
  33. $ip = $_SERVER['REMOTE_ADDR'];
  34. }
  35. return $ip;
  36. }
  37.  
  38. function new_user() {
  39. $insert = mysql_query ("INSERT INTO useronline(timestamp, ip) VALUES ('$this->timestamp', '$this->ip')");
  40. if (!$insert) {
  41. $this->error[$this->i] = "Unable to record new visitor\r\n";
  42. $this->i ++;
  43. }
  44. }
  45.  
  46. function delete_user() {
  47. $delete = mysql_query ("DELETE FROM useronline WHERE timestamp < ($this->timestamp - $this->timeout)");
  48. if (!$delete) {
  49. $this->error[$this->i] = "Unable to delete visitors";
  50. $this->i ++;
  51. }
  52. }
  53.  
  54. function count_users() {
  55. if (count($this->error) == 0) {
  56. $count = mysql_num_rows ( mysql_query("SELECT DISTINCT ip FROM useronline"));
  57. return $count;
  58. }
  59. }
  60.  
  61. }
  62.  
  63. ?>
Can any one tell me where my prob is?
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,102
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 139
ardav's Avatar
ardav ardav is offline Offline
Veteran Poster
 
-1
  #2
Oct 31st, 2009
I haven't looked closely at your code, but it looks as though you've left out a semicolon at the end of:

  1. $db = "users"
If you don't reply to your own thread or you can't find the solved link - you're off my Christmas list - permanently! Bah humbug!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 9
Reputation: foxwizzy is an unknown quantity at this point 
Solved Threads: 1
foxwizzy foxwizzy is offline Offline
Newbie Poster
 
0
  #3
Oct 31st, 2009
What wandres a small semicolon can do
the script is ok now thenx
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 130
Reputation: hemgoyal_1990 is an unknown quantity at this point 
Solved Threads: 8
hemgoyal_1990's Avatar
hemgoyal_1990 hemgoyal_1990 is offline Offline
Junior Poster
 
-1
  #4
Oct 31st, 2009
@above.
if your script is work and ur problem is solved then please mark this thread to solve.
http://www.kuchamancity.com
Hem Web Solution..
Behind Every Successful Man, There is an Untold Pain in His Heart.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 9
Reputation: foxwizzy is an unknown quantity at this point 
Solved Threads: 1
foxwizzy foxwizzy is offline Offline
Newbie Poster
 
0
  #5
Nov 1st, 2009
Now that i got the number of users on work i have another prob...
I am going to show the total number of registerd users(userID,user name,E-Mail)...and i dont now what this is :
"Notice: Undefined index: id in C:\wamp\www\Untitled-1.php on line 3"

The script works but thet line is buging me

this is line 3
  1. $myID = $_GET['id'];

This is the script :

  1. <?php
  2.  
  3. $myID = $_GET['id'];
  4.  
  5. $server = 'host';
  6. $username = 'dbuser';
  7. $password = 'dbpass';
  8. $database = 'db';
  9.  
  10. $connect = mysql_connect($server, $username, $password)
  11. or die("Error connecting: " . mysql_error());
  12. $select = mysql_select_db($database, $connect)
  13. or die("Error selecting DB: " . mysql_error());
  14.  
  15. // Select a specific entry
  16. if(strlen($myID) > 0) {
  17. $query = "SELECT * FROM users WHERE id='$myID'"; }
  18. // Select EVERY entry
  19. else {
  20. $query = "SELECT * FROM users"; }
  21. ?>
  22.  
  23. <center>
  24. <table border='1' cellspacing='0' cellpadding='2'>
  25. <tr>
  26. <th>ID</th>
  27. <th>Nume Utilizator</th>
  28. <th>E-Mail</th>
  29. </tr>
  30.  
  31. <?php
  32.  
  33. $run = mysql_query($query)
  34. or die("Error running query: " . mysql_error());
  35. while($row = mysql_fetch_array($run)) {
  36. print("\n<tr><td>{$row['ID']}</td>");
  37. print("\n<td>{$row['username']}</td>");
  38. print("\n<td>{$row['email']}</td></tr>"); }
  39.  
  40. ?>
  41.  
  42. </table>
  43. </center>
Last edited by foxwizzy; Nov 1st, 2009 at 5:43 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,102
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 139
ardav's Avatar
ardav ardav is offline Offline
Veteran Poster
 
-1
  #6
Nov 1st, 2009
Why is it bugging you?

You don't have any validation:
If expecting an integer, you should check to see that you are getting an integer. What happens if an id is not passed? You script will fail. What should you do in this case? Select a random id from the DB or show an error message before redirecting to another page?
Look up:

is_int() and isset()
Last edited by ardav; Nov 1st, 2009 at 6:09 am.
If you don't reply to your own thread or you can't find the solved link - you're off my Christmas list - permanently! Bah humbug!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the PHP Forum


Views: 319 | Replies: 5
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC