开发者

Storing&Retrieving Integer in/from MySQL Database

I have a problem with integers in MySQL. I am trying to update a cell which stores an integer. However, I have problem with the type of that cell. Actually it is type is set to int but when I retrive the data I always get 0 and I belive it is because of the problem about how I am trying to get it. Here is my code sample;

    function updateNumb($username) {


    $query = "SELECT `num` FROM `profiles` WHERE `nick`='" . $username . "'";
    $result = mysql_query($query, $this->conn) or die(mysql_error());

    $row = mysql_fetch_array($result, MYSQL_ASSOC);
    $row['num'] = $row['num'] + 1;
    $numb = (int)$row['num'] + 1;



//here I update the data.

    $query = "UPDATE `profiles` SET `num`=" . $numb . " WHERE `nick`='".$username."'";
    mysql_query($query, $this->conn) or die(mysql_error());

    return $numb;
}

Can it be because of mysql_fetch_array stuff? Or how could I overcom开发者_开发问答e this problem?


replace partisayisi with num


There is nothing wrong with the code you provided, maybe it's not doing what you really need, for example num is incremented twice, but there are no visible mistakes that would make it return 0, at least not in what we can see.

Make sure you provide valid username, try to echo your query before sending to mysql to see what it really looks like, maybe try this query yourself in mysql client or phpmyadmin to see what's going on.

Also if the only thing you need is to increment num for some user you can do it in one update, you don't need to use select to get that number:

UPDATE profiles set num=num+1 WHERE nick='somenick'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜