【问题标题】:How I insert and get values to and from database using Zend framework我如何使用 Zend 框架在数据库中插入和获取值
【发布时间】:2009-01-02 18:45:49
【问题描述】:

我如何使用 Zend 框架将我的用户名、密码值插入数据库。

以及我如何使用 zend 框架从数据库中获取值。

【问题讨论】:

    标签: php zend-framework


    【解决方案1】:

    您是否了解在 PHP 中使用数据库?在进入框架之前,你绝对应该start there。当您对 PHP 和数据库如何交互有了基本的了解后,将其转换为像 Zend 这样的优秀框架应该不会太困难。

    Zend 框架手册对everything it can do with databases 进行了相当全面的概述。

    【讨论】:

      【解决方案2】:

      /插入/ /** * 我假设您正在通过控制器传递数据 */

      class Application_Model_YourModelName extends Zend_Db_Table_Abstract {
      
      private static $_instance = null;
      protected $_name = 'YourModelName';
      
      public static function getinstance() {
          if (self::$_instance == null)
              self::$_instance = new Application_Model_YourModelName();
          return self::$_instance;
      }
      
      public function insertFunction(){
        if(func_num_args()>0){
            $userName = func_get_arg(0); // or some index if wanna pass through key value pair
           $password = md5(func_get_arg(1));
             $data = array('userName'=>$userName, 'password'=>$password);
               $insert = $this->insert($data);
        }
      }
      
      
      public function fetchFunction(){
        $sql = $this->select()
             ->where("*Your condition*")
             ;
         $result = $this->getAdapter->fetchAll(sql);
      }
      

      在安全方面使用 md5 功能不够安全,你可能想寻找 bcrypt 散列算法.. 这是相同的链接: http://framework.zend.com/manual/current/en/modules/zend.crypt.password.html

      【讨论】:

      【解决方案3】:
      /**
      *
      *To insert your values
      *
      * Here $uname and $password are values dynamically
      */
      
      $db = new Zend_Db(....);
      $data = array(
                    'vUserName'=>$uname,
                    'vPassword'=>md5($pwd)
      );
      
      $db->insert('tablename',$data);
      
      
      /*
      * 
      *To get the values 
      *
      */
      
      $sql = "SELECT * FROM <TABLENAME> WHERE vUserName = '".$uname."'";
      $data = $db->fetchAll($sql);
      return $data;
      

      【讨论】:

      • 为提高您的帖子质量,请说明此代码将如何/为何解决问题。
      【解决方案4】:
      $db = new Zend_Db(....);
      $data = array('username'=>'thomaschaaf', 'password'= md5('secret'));
      $db->insert('field', $data);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-06
        • 2023-04-06
        • 1970-01-01
        相关资源
        最近更新 更多