You should, for starters, get rid of the @
symbols in front of function calls. This will suppress errors caused by the given statement - and leave you wondering what is going wrong. While you are developing, you want all the information about your code you can get, especially errors. So, removing the @
(error suppression operator) is the first step.
Second, whenever you query your database, you have two steps. First you call mysql_query
. Then, after ensuring that mysql_query
returned anything, you call a fetch-type statement on the query object. Check out the docs for mysql_query
: http://php.net/manual/en/function.mysql-query.php There is a great, simple example there - see if you can spot where your code goes wrong.
All of that aside, don't need to jump through all those hoops to increment a database field...
mysql_query('UPDATE votes SET no = no+1 WHERE id='.$id);
For reference, a quick re-write of your sample code would look like this (using your approach rather than the one I mention above):
$dblocation = "localhost";
$dbname = "xx";
$dbuser = "xx";
$dbpasswd = "xx";
$dbcnx = mysql_connect($dblocation,$dbuser,$dbpasswd);
// handle a problem connecting to the database. You probably want to do something nicer than just "die" an error message
if (!$dbcnx)
die('Error connecting to database');
mysql_select_db($dbname, $dbcnx);
// not strictly neccesary to do this
mysql_query("SET NAMES utf8");
mysql_query("SET COLLATION_CONNECTION=utf8_bin");
$query = mysql_query('SELECT `no` FROM `votes` WHERE `id`='.$id);
// again, you'll probably want to do something nicer than blat out a text error
if (!$query)
die('Record not found');
$num = mysql_fetch_assoc($query);
// type your number
$num = (int) $num['no'];
// increment it
$num++;
mysql_query('UPDATE `votes` SET `no`='.$num.' WHERE `id`='.$id);