PHP PDO标准获取公式尽可能快速和安全


PHP PDO standard fetching formula as fast and safe as possible

多年来我一直在使用mySQL。大错。PDO绝对是要走的路,我已经学会了艰难的方式。

但是我还不太明白,也没有发现教程特别有用。

安全性和速度是我对mySQL的主要问题。如果我想说从中获取信息,标准PDO公式会是什么样子

$variable1 - SQL TABLE
$variable2 - FIELD TO MATCH
$variable3 - MATCH WITH THIS

然后假设每个找到的行中都有给定数量的字段,一旦找到它,它大概看起来像$row['field_name'];

如果我能根据这个例子看到这是如何完成的,我想我可能可以管理其余的。

另外,如果您知道一个很好的链接,例如w3schools为mySQL查询所做的工作,但对于PDO所做的,那么我也将不胜感激。

完整文档,它看起来像这样:

<?php
try {
//Start connection
    $pdo = new PDO("mysql:host=localhost;dbname=database_name", "user", "password");
/*
* Set attributes
*/
//Throw exceptions on errors
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//Disable prepared statements emulations
    $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    $table_name = "example_table";
    $field = "example_field";
    $value = "example_value";
    $query = <<<MySQL
    SELECT *
        FROM `$table_name`
        WHERE `$field` = :value
MySQL;
//Prepare the statement
    $stmt = $pdo->prepare($query);
//Bind values to placeholders
    $stmt->bindValue(":value", $value);
//Execute the statement
    $stmt->execute();
/*
* Fetching results
*/
//Fetch all results as an associative array.
    $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    die("An error has occurred! " . $e->getMessage());
}