-3

我需要重写我所有的代码。上次有人告诉我,我需要在 mysqli 或 PDO 之间进行选择。现在我选择了 PDO——但我必须说我什么都不懂。 .

例如我有这段代码:

//get the email
$email = mysql_real_escape_string($_POST['email']);

//mysql query to select field email if it's equal to the email that we check '
$result = mysql_fetch_array(mysql_query("SELECT email FROM business_members WHERE email = '".$email."'"));

//if number of rows fields is bigger them 0 that means it's NOT available '
if($result['email'] == $email){
//and we send 0 to the ajax request
echo "0";
}else{
//else if it's not bigger then 0, then it's available '
//and we send 1 to the ajax request
echo "1";
}

是否有任何网站可以帮助我更好地理解,我曾尝试在 php-net 上阅读,但它非常令人困惑..

4

2 回答 2

1

PDO 最好的地方在于它是面向对象的。因此,保持这种形式并充分利用它,我们可以创建一个 PDO CRUD 类来处理所有数据库查询等。

这是一个示例,可以添加自定义方法/功能以增强功能等:

<?php 
Class PDO_CRUD{
    private $db;

    function __construct($host,$dbname,$user,$pass){
        $this->dbhost = $host;
        $this->dbname = $dbname;
        $this->dbuser = $user;
        $this->dbpass = $pass;
    }

    private function connect(){
        if (!$this->db instanceof PDO){
            $this->db = new PDO('mysql:dbname='.$this->dbname.';host='.$this->dbhost, $this->dbuser, $this->dbpass);
            $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }
    }

    /*Raw Select*/
    public function rawQuery($sql){
        $this->connect();
        return $this->db->query($sql)->fetchAll(PDO::FETCH_ASSOC);
    }

    public function get($table,$fieldname=null, $id=null){
        $this->connect();
        $sql = "SELECT * FROM $table WHERE $fieldname = :id";
        $statement = $this->db->prepare($sql);
        $statement->bindParam(':id', $id, PDO::PARAM_STR);
        $statement->execute();
        return $statement->fetchAll(PDO::FETCH_ASSOC);
    }

    /*Insert*/
    public function put($table,$values){
        $this->connect();
        $fieldnames = array_keys($values[0]);
        $sql = "INSERT INTO $table ";
        $fields = '('.implode(' ,', $fieldnames).')';
        $bound = '(:'.implode(', :', $fieldnames).')';
        $sql .= $fields.' VALUES '.$bound;

        $statement = $this->db->prepare($sql);
        foreach($values as $vals){
            $statement->execute($vals);
        }
    }

    /*Update*/
    public function update($table,$fieldname, $value, $pk, $id){
        $this->connect();
        $sql = "UPDATE $table SET $fieldname = :value WHERE $pk = :id";
        $statement = $this->db->prepare($sql);
        $statement->bindParam(':id', $id, PDO::PARAM_STR);
        $statement->bindParam(':value', $value, PDO::PARAM_STR);
        $statement->execute();
    }

    /*Update Hits*/
    public function add_hit($table,$id){
        $this->connect();
        $sql = "UPDATE $table SET hits = hits + 1 WHERE url = :id";
        $statement = $this->db->prepare($sql);
        $statement->bindParam(':id', $id, PDO::PARAM_STR);
        $statement->execute();
    }

    /*Delete*/
    public function delete($table,$id){
        $this->connect();
        $sql = "DELETE FROM $table WHERE url = :id";
        $statement = $this->db->prepare($sql);
        $statement->bindParam(':id', $id, PDO::PARAM_STR);
        $statement->execute();
    }
}

//Then we have a nice way to access all our querys from one class.
//ini the model class
$model = new PDO_CRUD('localhost','yourDB','User','Password');

$insert = array(array('id'=>NULL,'somecol'=>'someval'));
$model->put('someTable',$insert);

//multiple inserts
$insert = array(array('id'=>NULL,'somecol'=>'someval123'),
                array('id'=>NULL,'somecol'=>'someval1234'),
                array('id'=>NULL,'somecol'=>'someval12345'));
$model->put('someTable',$insert);

//or delete a row
$model->delete('someTable',1);

//or a raw query
$model->rawQuery('DELETE FROM someTable');
?>
于 2012-05-28T09:42:45.347 回答
0

包含 PDO 函数的 PHP 文件:

<?php
class example {
    public function __construct() {
        $this->db = new PDO('mysql:host=localhost;dbname=testdb;', 'user', 'password');
    }

    public function checkMail($email) {
        // This is the prepared SQL statement
        // The values which you want to filter for in your WHERE clause, are replaced by ?
        $sql = "SELECT
                    email
                FROM
                    business_members
                WHERE
                    email = ?";

        // Prepare the statement
        $stmt = $this->db->prepare($sql);

        // Bind a value to a question mark (the 1 means, the position of occurence of the question mark)
        $stmt->bindParam(1, $email);

        // Query the db, output debug info if query failed
        if(!$stmt->execute()) {
            // Only for debugging, don't use in production
            var_dump($stmt->errorInfo());
        }

        // Load result to var
        $result = $stmt->fetchAll(PDO::FETCH_ASSOC);

        // Check amount of records
        if(count($result) > 0) {
            // Records found
            return 1;
        } else {
            // No records found
            return 0;
        }
    }
}
?>

主文件:

<?php
include 'includes/pdo.include.php';

$example = new example;

echo $example->checkMail($_POST['email']);
?>
于 2012-05-28T09:41:26.407 回答