Jump to content

SQL update


duncan_cowan

Recommended Posts

i have a script that shows the number of users online and it adds a row to a table with the users ip address, the time, the file from which the table was updated, and whether they are logged in or not. i have edited it so that if their ip address is already one of the rows then it will just update it so that it doesnt add an other row but it just keeps adding an other row!!the script is:

 <?include "config.php";$t_stamp = time();$timeout = $t_stamp - $to_secs;mysql_connect($server, $db_user, $db_pass) or die ("Useronline Database CONNECT Error");$check = mysql_query("SELECT * FROM CJ_UsersOnline WHERE ip='$REMOTE_ADDR'");$result = mysql_num_rows("$check");if ($result>="1") {mysql_query("UPDATE CJ_UsersOnline SET timestamp = '$t_stamp' WHERE ip = '$REMOTE_ADDR'") or die("Database UPDATE Error");} else {mysql_db_query($db, "INSERT INTO CJ_UsersOnline VALUES ('$t_stamp','$REMOTE_ADDR','$PHP_SELF','0')") or die("Database INSERT Error");}mysql_db_query($db, "DELETE FROM CJ_UsersOnline WHERE timestamp<$timeout") or die("Database DELETE Error");$resultt = mysql_db_query($db, "SELECT DISTINCT ip FROM CJ_UsersOnline WHERE file='$PHP_SELF'") or die("Database SELECT Error");$resultl = mysql_db_query($db, "SELECT DISTINCT ip FROM CJ_UsersOnline WHERE logged=1") or die("Database SELECT Error");$resultul = mysql_db_query($db, "SELECT DISTINCT ip FROM CJ_UsersOnline WHERE logged=0") or die("Database SELECT Error");$total = mysql_num_rows($resultt);$logged = mysql_num_rows($resultl);$ulogged = mysql_num_rows($resultul);mysql_close();if ($logged == 1){	echo "$logged    Member Online<br />";}else{	echo "$logged    Members Online<br />";}if ($ulogged == 1){	echo "$ulogged    Guest Online<br />";}else{	echo "$ulogged   Guests Online<br />";}if ($total == 1){	echo "$total    Person Online<br />";}else{	echo "$total    People Online<br />";}?>

please could some one tell me what is wrong?

Link to comment
Share on other sites

i am not sure if i am correct, i think i need to look on the full source code if it is available, i think u are intergating the number of usersonline with to show the IP address of the person too.

Link to comment
Share on other sites

that is all the sorce code apart from the file with the database information. All the script is meant to do is show how many users are logged in and how many are on the website and then the total.

Link to comment
Share on other sites

well u must set your database and include the config.php below and run the code.How it will be displayed.you are 1 of the <?code?> onlinein the place of <?code?> it will display the total users online.

<? $timestamp=time(); $timeout=$timestamp-$timeoutseconds; mysql_connect($server, $db_user, $db_pass) or die ("Useronline Database CONNECT Error"); mysql_db_query($database, "INSERT INTO useronline VALUES ('$timestamp','$REMOTE_ADDR','$PHP_SELF')") or die("Useronline Database INSERT Error"); mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout") or die("Useronline Database DELETE Error");$result=mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='$PHP_SELF'") or die("Useronline Database SELECT Error");$user =mysql_num_rows($result); mysql_close(); if ($user==1) {echo"$user User online";} else {echo"$user Users online";}?>
Link to comment
Share on other sites

Im Not Understanding!!?also when i run my script it says:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/i04wasp/public_html/protected/users_online/users.php on line 12
???
Link to comment
Share on other sites

I'm not exactly sure... but i'll try to correct your code in some places;

 <?include "config.php";$t_stamp = time();$timeout = $t_stamp - $to_secs;$msa=mysql_connect($server, $db_user, $db_pass) or die ("Useronline Database CONNECT Error");$check = mysql_query("SELECT * FROM CJ_UsersOnline WHERE ip='$REMOTE_ADDR'",$msa);$result = mysql_num_rows($check);if ($result>="1") {mysql_query("UPDATE CJ_UsersOnline SET `timestamp` = $t_stamp WHERE `ip` = '$REMOTE_ADDR'") or die("Database UPDATE Error");} else {mysql_query($db, "INSERT INTO CJ_UsersOnline VALUES ('$t_stamp','$REMOTE_ADDR','$PHP_SELF','0')",$msa) or die("Database INSERT Error");}mysql_query("DELETE FROM CJ_UsersOnline WHERE timestamp<$timeout",$msa) or die("Database DELETE Error");$resultt = mysql_query("SELECT DISTINCT ip FROM CJ_UsersOnline WHERE file='$PHP_SELF'",$msa) or die("Database SELECT Error");$resultl = mysql_query("SELECT DISTINCT ip FROM CJ_UsersOnline WHERE logged=1",$msa) or die("Database SELECT Error");$resultul = mysql_query("SELECT DISTINCT ip FROM CJ_UsersOnline WHERE logged=0",$msa) or die("Database SELECT Error");$total = mysql_num_rows($resultt);$logged = mysql_num_rows($resultl);$ulogged = mysql_num_rows($resultul);mysql_close();if ($logged == 1){	echo "$logged    Member Online<br />";}else{	echo "$logged    Members Online<br />";}if ($ulogged == 1){	echo "$ulogged    Guest Online<br />";}else{	echo "$ulogged   Guests Online<br />";}if ($total == 1){	echo "$total    Person Online<br />";}else{	echo "$total    People Online<br />";}?>

Dont use mysql_db_query... just use msql_query

Link to comment
Share on other sites

now it just says:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/i04wasp/public_html/protected/users_online/users.php on line 12Warning: Wrong parameter count for mysql_query() in /home/i04wasp/public_html/protected/users_online/users.php on line 20Database INSERT Error
???
Link to comment
Share on other sites

<?include "config.php";$t_stamp = time();$timeout = $t_stamp - $to_secs;$result=array();$msa=mysql_connect($server, $db_user, $db_pass) or die ("Useronline Database CONNECT Error");$check = mysql_query("SELECT * FROM CJ_UsersOnline WHERE ip='$REMOTE_ADDR'",$msa) or die(mysql_error());$result['check'] = mysql_num_rows($check);if ($result['check']>="1") {mysql_query("UPDATE CJ_UsersOnline SET `timestamp` = $t_stamp WHERE `ip` = '$REMOTE_ADDR'") or die("Database UPDATE Error");} else {mysql_query("INSERT INTO CJ_UsersOnline VALUES ($t_stamp,'$REMOTE_ADDR','$PHP_SELF',0)",$msa) or die("Database INSERT Error");//Numeric values dont need quotes around them.}mysql_query("DELETE FROM CJ_UsersOnline WHERE timestamp<$timeout",$msa) or die("Database DELETE Error");$result['file'] = mysql_query("SELECT DISTINCT ip FROM CJ_UsersOnline WHERE file='$PHP_SELF'",$msa) or die("Database SELECT Error");$result['logged'] = mysql_query("SELECT DISTINCT ip FROM CJ_UsersOnline WHERE logged=1",$msa) or die("Database SELECT Error");$result['not_loged'] = mysql_query("SELECT DISTINCT ip FROM CJ_UsersOnline WHERE logged=0",$msa) or die("Database SELECT Error");$total = mysql_num_rows($result['file']);$logged = mysql_num_rows($result['logged']);$ulogged = mysql_num_rows($result['not_loged']);mysql_close();if ($logged == 1){ echo "$logged   Member Online<br />";}else{ echo "$logged   Members Online<br />";}if ($ulogged == 1){ echo "$ulogged   Guest Online<br />";}else{ echo "$ulogged   Guests Online<br />";}if ($total == 1){ echo "$total   Person Online<br />";}else{ echo "$total   People Online<br />";}?>Try that now

Link to comment
Share on other sites

To make it easier why not just have it remove the row person where the coolumn ip = users ip, then add in the users ip with time. Then do the selection. I do believe there is a script I made a while ago. Just search for "display users" on the pph forum and look for a topic by me

Link to comment
Share on other sites

Well the in my above script is working perfectly.you donot need to do anything, just

<?include "config.php";$timestamp=time();$timeout=$timestamp-$timeoutseconds;mysql_connect($server, $db_user, $db_pass) or die ("Useronline Database CONNECT Error");mysql_db_query($database, "INSERT INTO useronline VALUES ('$timestamp','$REMOTE_ADDR','$PHP_SELF')") or die("Useronline Database INSERT Error");mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout") or die("Useronline Database DELETE Error");$result=mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='$PHP_SELF'") or die("Useronline Database SELECT Error");$user =mysql_num_rows($result);mysql_close();if ($user==1) {echo"$user User online";} else {echo"$user Users online";}?>
now try to run this script instead of the one you are running, and in the page where u want to display the usersonline just add these line of codeyou are 1 of the <?php include('thefilename.php');?> usersonline.if u want to see an example, go to aspin.com and in the top u would see like this.
Link to comment
Share on other sites

To make it easier why not just have it remove the row person where the coolumn ip = users ip, then add in the users ip with time. Then do the selection. I do believe there is a script I made a while ago. Just search for "display users" on the pph forum and look for a topic by me
I have already tried that but the script i used from the topic went all wrong!
<?include "config.php";$t_stamp = time();$timeout = $t_stamp - $to_secs;$result=array();$msa=mysql_connect($server, $db_user, $db_pass) or die ("Useronline Database CONNECT Error");$check = mysql_query("SELECT * FROM CJ_UsersOnline WHERE ip='$REMOTE_ADDR'",$msa) or die(mysql_error());$result['check'] = mysql_num_rows($check);if ($result['check']>="1") {mysql_query("UPDATE CJ_UsersOnline SET `timestamp` = $t_stamp WHERE `ip` = '$REMOTE_ADDR'") or die("Database UPDATE Error");} else {mysql_query("INSERT INTO CJ_UsersOnline VALUES ($t_stamp,'$REMOTE_ADDR','$PHP_SELF',0)",$msa) or die("Database INSERT Error");//Numeric values dont need quotes around them.}mysql_query("DELETE FROM CJ_UsersOnline WHERE timestamp<$timeout",$msa) or die("Database DELETE Error");$result['file'] = mysql_query("SELECT DISTINCT ip FROM CJ_UsersOnline WHERE file='$PHP_SELF'",$msa) or die("Database SELECT Error");$result['logged'] = mysql_query("SELECT DISTINCT ip FROM CJ_UsersOnline WHERE logged=1",$msa) or die("Database SELECT Error");$result['not_loged'] = mysql_query("SELECT DISTINCT ip FROM CJ_UsersOnline WHERE logged=0",$msa) or die("Database SELECT Error");$total = mysql_num_rows($result['file']);$logged = mysql_num_rows($result['logged']);$ulogged = mysql_num_rows($result['not_loged']);mysql_close();if ($logged == 1){ echo "$logged   Member Online<br />";}else{ echo "$logged   Members Online<br />";}if ($ulogged == 1){ echo "$ulogged   Guest Online<br />";}else{ echo "$ulogged   Guests Online<br />";}if ($total == 1){ echo "$total   Person Online<br />";}else{ echo "$total   People Online<br />";}?>Try that now
I have tried this but it now says :
No database selected
???
Link to comment
Share on other sites

you didnt do mysql_select_db();.. so

<?include "config.php";$t_stamp = time();$timeout = $t_stamp - $to_secs;$result=array();$msa=mysql_connect($server, $db_user, $db_pass) or die ("Useronline Database CONNECT Error");@mysql_select_db("DATABASE NAME!!!!!!") or die(mysql_error());$check = mysql_query("SELECT * FROM CJ_UsersOnline WHERE ip='$REMOTE_ADDR'",$msa) or die(mysql_error());$result['check'] = mysql_num_rows($check);if ($result['check']>="1") {mysql_query("UPDATE CJ_UsersOnline SET `timestamp` = $t_stamp WHERE `ip` = '$REMOTE_ADDR'") or die("Database UPDATE Error");} else {mysql_query("INSERT INTO CJ_UsersOnline VALUES ($t_stamp,'$REMOTE_ADDR','$PHP_SELF',0)",$msa) or die("Database INSERT Error");//Numeric values dont need quotes around them.}mysql_query("DELETE FROM CJ_UsersOnline WHERE timestamp<$timeout",$msa) or die("Database DELETE Error");$result['file'] = mysql_query("SELECT DISTINCT ip FROM CJ_UsersOnline WHERE file='$PHP_SELF'",$msa) or die("Database SELECT Error");$result['logged'] = mysql_query("SELECT DISTINCT ip FROM CJ_UsersOnline WHERE logged=1",$msa) or die("Database SELECT Error");$result['not_loged'] = mysql_query("SELECT DISTINCT ip FROM CJ_UsersOnline WHERE logged=0",$msa) or die("Database SELECT Error");$total = mysql_num_rows($result['file']);$logged = mysql_num_rows($result['logged']);$ulogged = mysql_num_rows($result['not_loged']);mysql_close();if ($logged == 1){echo "$logged   Member Online<br />";}else{echo "$logged   Members Online<br />";}if ($ulogged == 1){echo "$ulogged   Guest Online<br />";}else{echo "$ulogged   Guests Online<br />";}if ($total == 1){echo "$total   Person Online<br />";}else{echo "$total   People Online<br />";}?>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...