开箱即用魔术..用PDO

时间:2012-08-13 11:23:27

标签: php mysql pdo zend-server-ce

        try{
            $pdo = new PDO("mysql:host=localhost;dbname=name", 'user', 'pass');
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $statement = "SELECT id FROM users WHERE email LIKE ?";
            $stmt = $pdo->prepare($statement);
            $stmt->bindParam(1, htmlspecialchars($this->params['email']), PDO::PARAM_STR);
            $stmt->setFetchMode(PDO::FETCH_ASSOC);
            $row = $stmt->fetch();

        }catch (PDOException $e) {
            echo $e;
        }

        print_r($row[0]['id']);

        if(is_int($row[0]) > 0) {
            throw new Exception();
        } elseif(is_int($row[0]) > 0) {
            //Generating encryption
            $crypt = mcrypt_module_open(MCRYPT_RIJNDAEL_256, '', MCRYPT_MODE_CBC, '');
            $iv = mcrypt_create_iv(32, MCRYPT_RAND);
            $key = mcrypt_create_iv(32, MCRYPT_RAND);
            $password = mcrypt_create_iv(12, MCRYPT_RAND);
            mcrypt_generic_init($crypt, $key, $iv);
            $crypted = mcrypt_generic($crypt, $password);


            $activation = mcrypt_create_iv(128, MCRYPT_RAND);

            mcrypt_generic_deinit($crypt);




            try{
                $pdo = new PDO("mysql:host=localhost;dbname=name", 'user', 'pass');
                $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                $statement = "INSERT INTO users(name, password, cryptokey, cryptovector, email, activation_code, active, usertype) values(?, ?, ?, ?, ?, ?, 0, ?); ";
                $stmt = $pdo->prepare($statement);
                $stmt->bindParam(1, mysql_real_escape_string($this->params['name']), PDO::PARAM_STR);
                $stmt->bindParam(2, $password, PDO::PARAM_STR);
                $stmt->bindParam(3, $key, PDO::PARAM_STR);
                $stmt->bindParam(4, $iv, PDO::PARAM_STR);
                $stmt->bindParam(5, mysql_real_escape_string($this->params['email']), PDO::PARAM_STR);
                $stmt->bindParam(6, $activation, PDO::PARAM_STR);
                $stmt->bindParam(7, mysql_real_escape_string($this->params['acctype']), PDO::PARAM_STR);

                $stmt = null;

            }catch (PDOException $e) {
                print_r($e);
            }


            $smarty = new Smarty();

            $smarty->assign('NAMES',$this->params['name']);
            $smarty->assign('UNIT', $this->params['acctype']);
            $smarty->assign('KEYPASS',$password);
            $smarty->assign('NAMES',$this->params['name']);
            $smarty->assign('ACTIVATION_LINK','https://localhost/profile.php?action=activate&key='.urlencode($activation).'&email='.urlencode($this->params['email']));

            $msgBody = $smarty->fetch('templates/default/mail.html');

            try {
                $mail = new PHPMailer();

                $mail->Host = "server";
                $mail->SMTPAuth = TRUE;
                $mail->Host = "server";
                $mail->Port = 25;
                $mail->Username = "email";
                $mail->Password = "password";

                $mail->AddAddress($this->params['email']);
                $mail->SetFrom('test@example', 'test Services');

                $mail->Subject = 'Confirm registration and login credentials.';
                $mail->MsgHTML($msgBody);    // attachment
                #$mail->Send();

                echo 'localhost/profile.php?action=verify&key='.base64_encode($activation).'&email='.urlencode($this->params['email']);
            } catch (phpmailerException $e) {
                #echo $e; //Pretty error messages from PHPMailer
                print_r($e);
            } catch (Exception $e) {
                echo $e; //Boring error messages from anything else!
                print_r($e);
            }


        }

    }

所以这是我的身份验证方法。 __construct($ arg1,$ arg2); $ arg1获取动作和$ arg2,它是$ self :: params获取它需要的变量,在这种情况下是电子邮件名称和密码。它打印激活链接用于测试目的,当它出现时,我看到它后面的Array()。具有isset($ row)的if语句没有执行它所拥有的操作,因为它告诉我的脚本已设置行。当我print_r它没有看到...当echo我看到Array()....我使用Zend服务器CE使用PHP 5.4我有一个结构如下的表:

  

id, - name, - password, - cryptokey, - cryptovector, - email, - activation_code, - active, - usertype
  1, - J.Smith, - pwd, - key, - vector, - some @ mail, - 激活码, - 0, - 候选人

1 个答案:

答案 0 :(得分:1)

您错过了代码中的$stmt->execute();。这是我在eclipse中碰巧遇到的代码示例:

$sql="select id from users where userName=:userName";
$this->prepared = $this->mysqlAccess->con->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$this->prepared->execute(array(':userName' => $this->username));

准备语句时,需要先执行它,然后才能查询结果。