ZF2框架连接数据库的方法

控制器:

<?php
namespace Application\Controller;
error_reporting(E_ALL || ~E_NOTICE);
session_start();
use Application\Model\NewsModel;
use Zend\Mvc\Controller\AbstractActionController;

use Zend\View\Model\JsonModel;
use Zend\View\Model\ViewModel;

class IndexController extends AbstractActionController{

public function indexAction()
{
   return new ViewModel();
}
public function huoquAction()
{

    $request = $this->getRequest();
    $zhanghao = $request->getPost('zhanghao');//获取页面的账号
    $mima = $request->getPost('mima');//获取页面的密码
    if($zhanghao==""||$mima=="")
    {
        return new JsonModel(array('results'=>false));
    }
    else{
        //链接数据库的驱动,相当于引入一个类
        $_SESSION['zhanghao']=$zhanghao;

        $dbNewModel = new NewsModel($this->getDbAdapter());
        $getList = $dbNewModel->getList();
        //打印从数据库中获取到的值
       //从数据库中查找之后返回一个变量,如果该变量是空值,则输进去的账号并不存在,因为我是通过账号进行查找
        if(empty($getList['msg']))
           {
               //说明没有该用户
                //header('Location:http://localhost/application/index');
               return new JsonModel(array('results'=>false));//返回错误的值
          }else{
            //说明通过数据库查找之后有返回值
               $zhanghao1=$getList['msg']['0']['zhanghao'];//获取数据库那边返回来的账号
                $mima1=$getList['msg']['0']['mima'];//获取数据库那边返回回来的密码
            //判断账号密码是否都相等
            if(($zhanghao==$zhanghao1)&&($mima=$mima1))
            {
                return new JsonModel(array('results'=>true));//返回正确的值
            }
            else{
                return new JsonModel(array('results'=>false));//说明账号密码有一个输入错了,肯定 不给你登录啊
            }
        }
   // return new JsonModel(array('results' => true));
}
}

//定义注册页面
public function signAction()
{
        //return new ViewModel();
        //获取表单的值
        $request = $this->getrequest();
        $name1 = $request->getPost('name');
        //var_dump($name1);
    //exit;
        $password1 = $request->getPost('password');
        // echo $password1;
        //这里是连接数据库驱动 这边相当于引用一个类
        $dbNewModel = new NewsModel($this->getDbAdapter());
        $insertList = $dbNewModel->insertList();
        $name2 = $insertList['msg'][0]['zhanghao'];
        $password2 = $insertList['msg'][0]['mima'];

        // echo __DIR__.'77777';
    //判断该用户是否是已经注册的了

    if($name1=="")
    {

    }
    else if($name1==$name2)
    {
        //echo "该用户已经是注册了的!";
        echo "<script>alert('该用户是已经注册了的,请重新注册!');</script>";
        sleep(2);
        header('Location:http://zf11.com/application/sign');
        exit;
    }
else{
    //用session 将数据传到数据库中的操作中去
    //实现用户的添加
   // echo "进行添加!";
//    $_SESSION['name1']=$name1;
//    $_SESSION['password1']=$password1;
    $dbNewModel = new NewsModel($this->getDbAdapter());
    $insert1 = $dbNewModel->insert1(array('name'=>$name1,'pwd'=>$password1));
   exit;
}
    }
public function aaaAction()
{
    return new ViewModel();
}
    public function bbbAction()
    {
        return new ViewModel;
    }
}


?>

Model文件夹下的NewsModel文件

<?php
namespace Application\Model;
use Zend\Db\Adapter\Adapter;
use Zend\Db\Sql\Sql;
use Zend\Db\ResultSet\ResultSet;
class NewsModel {
    protected $adapter;
    /**
     * 构造函数
     * @param Array $config 数据库连接配置 
     */
    //这是一个构造函数,调用这个类的时候就会自动连接数据库驱动
    public function __construct($config=null)
    {
        if($config==null)
            $this->adapter = $this->getEvent()->getApplication()->getServiceManager()->get('Zend\Db\Adapter\Adapter');
        else
            //把他赋值给$this->adapter 结果是一样的
            $this->adapter = $config;
    }

    //这个函数被调用了
    public function getList(){
        $c=$_SESSION['zhanghao'];//获取表单传过来的一个账号的值进行数据库中的判断
        $sql = "select * from account where zhanghao='$c' ";
        //var_dump("333");
        //exit;
        //$sql = "select * from account";
        //再执行数据库的查询操作
        $statement = $this->adapter->createStatement($sql);
        $result = $statement->execute();
        $resultSet = new ResultSet;
        $resultSet->initialize($result);
        //var_dump(array('result' => true, 'msg' => $resultSet->toArray()));
        return array('result' => true, 'msg' => $resultSet->toArray());

    }
    //查询表单的值
   public function insertList(){
        $sql = "select * from account";
        //再执行数据库的查询操作
        $statement = $this->adapter->createStatement($sql);
        $result = $statement->execute();
        $resultSet = new ResultSet;
        $resultSet->initialize($result);
        return array('result' => true, 'msg' => $resultSet->toArray());
    }
    //添加用户到数据库中
    public function insert1($arrInfo){
        var_dump($arrInfo);
        $a=$arrInfo['name'];
        $b=$arrInfo['pwd'];
        $sql = "insert into account (zhanghao,mima) values('$a','$b')";
        $statement = $this->adapter->createStatement($sql);
     $result = $statement->execute();

       header('Location:http://zf11.com/application/index');
//        $a=$_SESSION['name1'];
//        $b=$_SESSION['password1'];
//        $sql = "insert into account (zhanghao,mima) values($a,$b)";
//        //再执行数据库的查询操作
//        $statement = $this->adapter->createStatement($sql);
//        $result = $statement->execute();
//        header('Location:http://localhost/application/index');
        exit;
        //$resultSet = new ResultSet;
        //$resultSet->initialize($result);
        //return array('result' => true, 'msg' => $resultSet->toArray());
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值