※ 引述《Relent (遗憾)》之铭言:
: 我在php中做更新纪录的动作
: 根据print出来的sql语法如下
: update `tableS` set `history`='[{"time":"2016-07-14
: 18:24:22","count":0,"link":"http://aaa.bbb.cc"}]' where account='110101'
: 只是我的资料表history字段里面的资料,却变成了这样
: [{"time":"2016-07-14
: 18:24:22","count":0,"link":"http://aaa.bbb.cc"},
: {"time":"2016-07-14
: 18:24:22","count":0,"link":"http://aaa.bbb.cc"},
: {"time":"2016-07-14
: 18:24:22","count":0,"link":"http://aaa.bbb.cc"},
: {"time":"2016-07-14
: 18:24:22","count":0,"link":"http://aaa.bbb.cc"}]
: 多出了3笔同样的纪录
: 想请问一下各位大大这种状况是因为什么呢
: 程式码如下,
: [code]
: $count=0;
: $bitStr="1101010101111010";
: while($count<30 && strlen($bitStr)>0){
: $percent=0.1;
: $obj=new stdClass();
: $obj->time=$checkDate;
: $obj->count=$count*$percent;
: $obj->link=$url;
: $arr=array();
: array_push($arr,$obj);
: $str=json_encode($arr);
: $sql="update tableS set history='".$str."' where account='".$bitStr."'";
: echo $sql;
: mysql_query($sql);
: $count++;
: $bitStr=substr($bitStr,0,-1);
: }
: [/code]
1. 请爱用PDO的parameters binding, 不然遇到SQL injection...
2. 储存时间最好用timestamp, 不然也期望你是UTC time..否则换server = gg
3. 你如果在php5.5+其实可以直接$str = json_encode([$obj]);
省去
$arr = array();
array_push($arr,$obj);
4. 你的110101010111101 才16个数字, 为什么要while($count<30)?
$bitStrLength = strlen($bitStr);
for($i = 0, $i <= strlen($bitStrLength), $i++)
5. 照上面的code来看不应该会output3个一样的, 但是我如果是你会这样写
{code:php}
$count = 0;
$percent = 0.1;
$bitStr = "1101010101111010";
$bitStrLength = strlen($bitStr);
for ($i = 0; $i < $bitStrLength; $i++) {
// don't really know why you storing objects into json but whatever
$obj = new stdClass();
$obj->time = $checkDate;
$obj->count = $i * $percent;
$obj->link = $url;
$str = json_encode([$obj]);
$sql = $sql="update tableS set history='".$str."' where account='".$bitStr."'";
echo $sql . "\n";
$bitStr = substr($bitStr, 0, -1);
}
{code}