The problem that the save() method returns a value of 0 when the data is updated using ThinkPHP

When using the TP framework to operate the database, the save() method is used to update the data, but it is found that there is no data update. After executing save(), the returned result is 0, which makes the judgment result wrong!


Originally my approach was this:

       $res = model used->where("condition")->save(array of updated data);

              if(  $res  ){      

                  $this->success('Success!');die;

              }else{

                  $this->error('Failure!');die;

              }


Later, there are official solutions in the manual:

The manual has this sentence:


That's it : the return value of save() is the number of records affected, and identity needs to be used to judge the result .

   So I tested the water non-stop, and changed to do this:

       $res = model used->where("condition")->save(array of updated data);

              if(  $res   !== false  ){      

                  $this->success('Success!');die;

              }else{

                  $this->error('Failure!');die;

              }

     The result is really a smiley face:

     


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324872759&siteId=291194637