Jump to content

Error with script


duncan_cowan

Recommended Posts

HiI have written a script to count users using a cookie stored on the users computer containing a random number and then storing the number in a database. then the script counts the number of rows but when i run it, it displays an error.script:

<?php$random = rand(60000000000000000000000, 90000000000000000000000); // between 0 and 100 inclusiveif (isset($_COOKIE["user_online"])){$sql_script = 'SELECT * FROM users-online';$count_sql = mysql_query($sql_script);$users_online = mysql_num_rows($count_sql);} else {setcookie("user_online", "$random");mysql_query("INSERT INTO users-online VALUES ('".$random."', '".$_SERVER['REMOTE_ADDR']."')");}?>

and the error:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/i04wasp/public_html/protected/users-online.php on line 14
Please could someone help??thanks in advance.Duncan
Link to comment
Share on other sites

In your insert query, you didn not include the field names:

mysql_query("INSERT INTO users-online (field1,field2) VALUES ('".$random."', '".$_SERVER['REMOTE_ADDR']."')");

Rename field1 and field2 obviouslly. I assume you have connected to the internet earlier in your script?

Link to comment
Share on other sites

It's not required to list the fields, if you don't list them then it will use the fields in the order they are listed in the table.I think the error is with your table name. The name users-online to the server looks like "users minus online". You probably want to put it in backquotes.

$sql_script = 'SELECT * FROM `users-online`';

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...