View Full Version : Counter
blind--angel
02-21-2003, 07:19 PM
Hi, i tried to make a counter and i keep getting the error
Parse error: parse error, unexpected T_LNUMBER in c:\apache\htdocs\james\counter\text.php on line 11
any ideas anyone? thx in adv.
counter
+----+-------+
| id | Count |
+----+-------+
| 1 | 0 |
+----+-------+
<?
$host = "localhost";
$login_name = "user";
$password = "";
MySQL_connect("$host","$login_name","$password");
MySQL_select_db("test1") or die("Could not select database");
$query = "SELECT * FROM counter WHERE id = "1";
mysql_query($query);
$count = $rows['Count'];
$counted = "$count"+1;
$sql = "UPDATE counter SET Count = "$counted" WHERE name = "visted";
$result = mysql_query($sql);
echo "$counted";
MySQL_close();
}
?>
jollyfactory
02-22-2003, 12:25 AM
I've edited the code..
<?
$host = "localhost";
$login_name = "user";
$password = "";
mysql_connect("$host","$login_name","$password");
mysql_select_db("test1") or die("Could not select database");
$query = "SELECT * FROM counter WHERE id = "1";
$result = mysql_query($query);
$rows = mysql_fetch_array($result);
$count = $rows['Count'];
$count++;
$sql = "UPDATE counter SET
Count = "$count"
WHERE id = "1";
$result = mysql_query($sql);
echo "$counted";
mysql_close();
}
?>
okay the things in bold is what I changed.
Firstly, its better coding practice to type MySQL as "mysql" e.g. mysql_close(); (instead of how you type MySQL_close();) but it's just good coding practice.
The problems I found is that you did not place the first query into an array. You wanted to increment $count so all you do is type $count++; to do that. The last error is your where statement in the second query. It says WHERE name = "visted" and your table above has no "name" column.
I've edited your code and it should work. Hope this helps.
kevin
02-22-2003, 02:44 AM
I'm not much of a PHP person, but in your code this line looks suspect to me, note the uneven amount of double quotes:
$sql = "UPDATE counter SET Count = "$counted" WHERE name = "visted";
scoutt
02-22-2003, 12:15 PM
kevin is correct, you can't even use double quotes like that. you have to use single quotes in the query. bu twhy do all that when you can do just this, plus you have other problems. don't put variable in quotes at all.
$counted = "$count"+1;
will not work, just do this
$count++;
but here is a more effiecent way of do ing what you want.
<?
$host = "localhost";
$login_name = "user";
$password = "";
mysql_connect("$host","$login_name","$password");
mysql_select_db("test1") or die("Could not select database");
$sql = "UPDATE counter SET Count = 'Count + 1' WHERE name = 'visted' ";
$result = mysql_query($sql);
$query = "SELECT * FROM counter";
$result = mysql_query($query);
$rows = mysql_fetch_array($result);
echo $rows['Count'];
mysql_close();
}
?>
make mysql work for you instead of your code slowing you down. also how can you echo $counted when there is no such variable? that should work but curious as to how many couters you have? if only 1 then why have the where clause in the query? also not sure as to where you are getting "visted" from?
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.