Home  >  Article  >  Backend Development  >  ThinkPHP操作数据库的有关问题

ThinkPHP操作数据库的有关问题

WBOY
WBOYOriginal
2016-06-13 11:10:30817browse

ThinkPHP操作数据库的问题
很诡异,无法操作数据库

class TestAction  extends Action {
    function index() {
        $data=array('username'=>'sunhu',
            'email'=>'asd',
            'content'=>'hello!'
        );
        $list = M("Guestbook")->add($data);
        if(!$list) {
            echo 0;
        }else {
            echo 1;
        }

    }
}


?>

浏览器输出为0


------解决方案--------------------
引用:
class TestAction extends Action {
  function index() {
  $data=array('username'=>'sunhu',
  'email'=>'asd',
  'content'=>'hello!'
  );
  $Gusetbook=M(Guestbook);
  $Gus……


请不要误导人家!你写的这一句 $Gusetbook->create();是没必要的,因为后面用的是add来添加数据的
难道你觉得楼主这样写
$list = M("Guestbook")->add($data);
  if(!$list) {
  echo 0;
  }else {
  echo 1;
  }
跟你说的
$Guestbook->add($data);//这边只是个插入操作,并没有数据返回的,如果你想有提示的话可以这样写
  if($Guestbook->add($data);) {
  $this->success("操作成功");
  }else {
  $this->error("操作失败");
  }
不一样??

------解决方案--------------------
我个人觉得有可能问题出在模型的实例化上。
如果你自定义的模型类跟标准配置不同,例如说用$trueTableName之类的定义了表名。这时候使用M方法可能会无法正常的实例化模型。
你可以先尝试使用D方法实例化
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn