user6235048 user6235048 - 7 months ago 12
SQL Question

mysql_query() returns something strange

I want to select one field (

MessageCounter
) from my database. Its type is
int(11)
. And I want to increase it.

Here's how I try to select it:

$q = "SELECT MessageCounter FROM " . TBL_USERS . " WHERE username = '$username'";
$result = mysql_query($q, $this->connection);


then I try to add
1
to it:

$messagecount = $result + 1;
$field = "MessageCounter";

$q = "UPDATE " . TBL_USERS . " SET " . $field . " = '$messagecount' WHERE username = '$username'";
return mysql_query($q, $this->connection);


And in the database it updates to
19
. If I add other number instead of
1
, say
3
, I get
21
. So the
$result
is somehow equal to
18
.
HOWEVER, if I try to update the database with the same unchanged result - it updates the field to
0
.

Does anyone have any idea what is happening?

Answer

You cannot add 1 to $result - first you need to fetch the value out of it:

$row = mysql_fetch_row($result);
$messagecount = $row[0] + 1;`

BTW - at this stage of learning, you should abandon the deprecated mysql_ functions and switch to mysqli or PDO instead. Do it right now.